Android ApiDemos示例解析(19):App->Alarm->Alarm Controller

Alarm Controller演示如何在Android應用中使用Alarm事件,其功能和java.util.Timer ,TimerTask類似。但Alarm可以即使當前應用退出後也可以做到Schedule一個任務在指定的時刻執行。

AlarmManager 用於管理Alarm事件,支持單次執行或重復執行。 和大都數Android服務一樣,AlarmManager也是通過getSystemService來獲取服務對象:

[java] 
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); 
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事件代碼如下:

 

[java] 
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); 
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一個重復事件代碼如下:

[java] 
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); 
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事件。

[java]
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); 
Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
 0, intent, 0);  www.aiwalls.com
 
// And cancel the alarm.
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(sender);

註:如果你沒有Cancel這個多次Alarm事件,每隔15秒屏幕上都會顯示一個消息,即使你退出這個例子或是啟動其它應用,直到Reboot之後才中止。

 作者:mapdigit
 
 

發佈留言