我需要设置,将触发一次重复报警,启动一个通知。
I need to set a repeating alarm that will once fired, launch a notification.
步骤:
我不明朗的报警系统是如何工作的。报警probaly应该保持沉默,可能与振动。是直截了当设置?
I'm uncertain of how the alarm system works. The alarm probaly should be silent, maybe with vibration. Is that straight forward to setup?
我是否需要服务或将广播reciever做的工作?
Do I need a service or will a broadcast reciever do the job?
基本上我只需要几个指针。我在思考这个正确?是否有一个教程在那里(我还没有发现任何东西)。先谢谢了。
Basically I just need a few pointers. Am I thinking about this correctly? Is there a tutorial out there (I haven't found anything). Thanks in advance.
推荐答案下面是我如何在我的应用程序使用AlarmService的演练。
Here is a walkthrough on how I use an AlarmService in my app.
设置一个AlarmManager在x分钟火。
Set up an AlarmManager to fire in x minutes.
在响应报警,启动服务。
In response to the alarm, start a service.
创建您的通知,并让您的服务为自己设定了一个新的报警在另一个x分钟再次触发。
Create your notification and have your service set itself up with a new Alarm to fire again in another x minutes.
该服务自行关闭。
1
Intent alarmIntent = new Intent(this, MyAlarm.class); long scTime = 60* 10000;// 10 minutes PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + scTime, pendingIntent);2
public class MyAlarm extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("Alarm Recieved!", "YAAAY"); Intent i = new Intent(context, InviteService.class); context.startService(i); } }3。
public class InviteService extends IntentService { /** * A constructor is required, and must call the super IntentService(String) * constructor with a name for the worker thread. */ public InviteService() { super("InviteService"); } /** * The IntentService calls this method from the default worker thread with * the intent that started the service. When this method returns, IntentService * stops the service, as appropriate. */ @Override protected void onHandleIntent(Intent intent) { String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); int icon = R.drawable.logo; CharSequence tickerText = "New Invite!"; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when); notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.defaults |= Notification.DEFAULT_VIBRATE; Context context = getApplicationContext(); CharSequence contentTitle = "Title"; CharSequence contentText = "Text"; Intent notificationIntent = new Intent(this, Destination.class); Bundle partyBundle = new Bundle(); PendingIntent contentIntent = PendingIntent.getActivity(this, SOME_ID, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); int NOTIFICATION_ID = SOME_ID; Log.d("NOTIFICATION_ID", "" + NOTIFICATION_ID); mNotificationManager.notify(NOTIFICATION_ID, notification);4(在同一个班)
4.(In the Same Class)
Intent alarmIntent = new Intent(this, MyAlarm.class); long scTime = 60*1000;//mins PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + scTime, pendingIntent); stopService(intent); } }希望这有助于!
Hope this helps!
修改
为什么要使用一个服务?
Why use a Service?
这不是明智的做法是做多的处理在广播接收器。虽然你可以在BroadcastReciever做一些处理,它是安全做一个服务,你可以找到在这个计算器问题的广播接收器VS服务
It's not wise to do much processing in the BroadcastReceiver. Although you can do some processing in the BroadcastReciever, it is safer to do this in a Service, you can find some information in this StackOverflow question BroadcastReceiver vs Service
更多推荐
如何获得警报触发通知
发布评论