当前位置:操作系统 > 安卓/Android >>

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.zzzyk.com
 
// And cancel the alarm.
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(sender);

注:如果你没有Cancel这个多次Alarm事件,每隔15秒屏幕上都会显示一个消息,即使你退出这个例子或是启动其它应用,直到Reboot之后才中止。

 作者:mapdigit
 
 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,