AlarmManager是Android中的一种系统级别的提醒服务,它会为我们在特定的时刻广播一个指定的Intent。而使用Intent的时候,我们还需要它执行一个动作,如startActivity,startService,startBroadcast,才能使Intent有用。通常我们使用PendingIntent,它可以理解为对Intent的封装,包含了指定的动作。

 

我们可以通过PendingIntent的静态方法得到一个PendingIntent对象,如下:

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);  

 

使用PendingIntent的getBroadcast (Context context, int requestCode, Intent intent, int flags)方法可以得到一个发送广播动作的PendingIntent对象。其中getBroadcast的第4个参数可以为以下4个常量或其他支持使用Intent.fillIn()来控制它的变量:

FLAG_CANCEL_CURRENT:如果描述的PendingIntent对象已经存在时,会先取消当前的PendingIntent对象再生成新的。

FLAG_NO_CREATE:如果描述的PendingIntent对象不存在,它会返回null而不是去创建它。

FLAG_ONE_SHOT:创建的PendingIntent对象只使用一次。

FLAG_UPDATE_CURRENT:如果描述的PendingIntent对象存在,则保留它,并将新的PendingIntent对象的数据替换进去。

 

接下来看AlarmManager,我们通过以下代码来取得AlarmManager对象。

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);  

AlarmManager对象中常用的方法有三个:

 

1、set(int type,long startTime,PendingIntent pi),用于设置一次闹钟。

2、setRepeating(int type,long startTime,long intervalTime,PendingIntent pi),用于设置重复闹钟。

3、setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi),同样是用于设置重复闹钟,但是它是不准确的,相对于第二个方法,也更加节能。因为系统会将差不多的闹钟进行合并,以避免在不必要地唤醒设备。

在上面的三个方法中,type为闹钟的类型,它可以使用以下四个常量:

ELAPSED_REALTIME:闹钟在睡眠状态下不可用,使用的是相对系统启动时间。

ELAPSED_REALTIME_WAKEUP:闹钟在睡眠状态下可用,使用的是相对系统启动时间。

RTC:闹钟在睡眠状态下不可用,使用的是真实时间。

RTC_WAKEUP:闹钟在睡眠状态下可用,使用的是真实时间。

startTime为闹钟开始时间。

intervalTime为闹钟间隔,在第三个方法中,内置的几个变量如下:

INTERVAL_FIFTEEN_MINUTES 
INTERVAL_HALF_HOUR 
INTERVAL_HOUR 
INTERVAL_HALF_DAY 
INTERVAL_DAY

 

如果我们设定的是发送广播的闹钟,我们还需要写一个广播接收器,并对其进行注册,它才会在闹钟开始的时候接收到广播。

如果要设定启动Activity或Service的闹钟,则在创建PendingIntent的时候,首先Intent对象需设定指定的Activity或Service的class对象,然后对应的调用PendingIntent.getActivity()或PendingIntent.getService()方法。

下面以设置发送广播的闹钟代码实例来看AlarmManager的使用:

首先设定一个闹钟:

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. Intent intent = new Intent("pw.msdx.ACTION_SEND");  
  2. PendingIntent sendIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
  3. AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);  
  4. am.cancel(sendIntent);  
  5. am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60 * 10 * 1000, sendIntent);  

在上面的例子中,就会从当前的时间开始,每10分钟启动一次闹钟提醒。需要注意的是,如果设定的开始时间已经过去,它会马上启动闹钟提醒。

 

接下来需要写一个广播接收器来接收这个广播并进行处理。

代码如下:

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public class SendReceiver extends BroadcastReceiver {  
  2.     public final static String ACTION_SEND = "pw.msdx.ACTION_SEND<span style="font-family: Arial, Helvetica, sans-serif;">";</span>  
  3.   
  4.     @Override  
  5.     public void onReceive(final Context context, Intent intent) {  
  6.         String action = intent.getAction();  
  7.         if (ACTION_SEND.equals(action)) {  
  8.             Log.i("SendReceiver", "send a message");  
  9.         }  
  10.     }  
  11. }  


然后我们还需要注册这个广播接收器,这样它才能接收到广播。这里采用的是静态注册的方法,在AndroidManifest里进行注册:

 

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <receiver  
  2.     android:name=".SendReceiver"  
  3.     android:enabled="true"  
  4.     android:exported="true"  
  5.      >  
  6.     <intent-filter>  
  7.         <action android:name="pw.msdx.ACTION_SEND"/>  
  8.     </intent-filter>  
  9. </receiver>  
posted on 2017-04-10 16:58  Snow〃冰激凌  阅读(5607)  评论(0编辑  收藏  举报