标签:
Alarm Controller演示如何在Android应用中使用Alarm事件,其功能和java.util.Timer ,TimerTask类似。但Alarm可以即使当前应用退出后也可以做到Schedule一个任务在指定的时刻执行。
AlarmManager 用于管理Alarm事件,支持单次执行或重复执行。 和大都数Android服务一样,AlarmManager也是通过getSystemService来获取服务对象:
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
与TimerTask对应的任务描述类型为PendingIntent,PendingIntent描述了将要执行的 Intent,PendingIntent没有提供构造函数,需要通过static 函数getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), getService(Context, int, Intent, int) 来或得想要执行的Activity,Broadcast,Service描述。
本例中是取得对Broadcast OneShotAlarm 和RepeatingAlarm的描述,分别对应于单次执行时执行的Broadcast事件和多次执行时Broadcast事件,它们在 AndroidManifest.xml定义为Broadcast Receiver:
<receiver android:name=”.app.OneShotAlarm” android:process=”:remote” /> <receiver android:name=”.app.RepeatingAlarm” android:process=”:remote” />
Schedule单次Alarm事件代码如下:
// When the alarm goes off, we want to broadcast an Intent to our // BroadcastReceiver. Here we make an Intent with an explicit class // name to have our own receiver (which has been published in // AndroidManifest.xml) instantiated and called, and then create an // IntentSender to have the intent executed as a broadcast. Intent intent = new Intent(AlarmController.this, OneShotAlarm.class); PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, 0, intent, 0); // We want the alarm to go off 30 seconds from now. Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.add(Calendar.SECOND, 30); // Schedule the alarm! AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
其中sender为对发给Broadcast Receiver OneShotAlarm的Intent的描述,当到达指定的时间(例子中为30秒),AlarmManager将给OneShotAlarm发出一个 Broadcast Intent,OneShotAlarm接到后,将使用Toast在屏幕上显示一个消息。 如果你多次点击“One Shot Alarm”并不会Schedule多个Alarm事件,这是因为Schedule同一个Sender对象,后一次将取消上此Scheduled的事件。
Schedule一个重复事件代码如下:
// When the alarm goes off, we want to broadcast an Intent to our // BroadcastReceiver. Here we make an Intent with an explicit class // name to have our own receiver (which has been published in // AndroidManifest.xml) instantiated and called, and then create an // IntentSender to have the intent executed as a broadcast. // Note that unlike above, this IntentSender is configured to // allow itself to be sent multiple times. Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class); PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, 0, intent, 0); // We want the alarm to go off 30 seconds from now. long firstTime = SystemClock.elapsedRealtime(); firstTime += 15*1000; // Schedule the alarm! AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 15*1000, sender);
上述代码每15秒给RepeatingAlarm 发出一个Broadcast事件,RepeatingAlarm接受到后,也在屏幕上显示一个消息。
对于与Schedule的事件,单次或多次的,都可以调用AlarmManager 的cancel方法取消Schedule的Alarm事件,下面代码取消多次Alarm事件。
// Create the same intent, and thus a matching IntentSender, for // the one that was scheduled. Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class); PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, 0, intent, 0); // And cancel the alarm. AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.cancel(sender);
注:如果你没有Cancel这个多次Alarm事件,每隔15秒屏幕上都会显示一个消息,即使你退出这个例子或是启动其它应用,直到Reboot之后才中止。
标签:
原文地址:http://www.cnblogs.com/dongdong230/p/4316475.html