转自:http://blog.csdn.net/maosidiaoxian/article/details/21776697


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

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

[java] view plain copy
  1. PendingIntentpi=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
  1. AlarmManageram=(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
  1. Intentintent=newIntent("pw.msdx.ACTION_SEND");
  2. PendingIntentsendIntent=PendingIntent.getBroadcast(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
  3. AlarmManageram=(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
  1. publicclassSendReceiverextendsBroadcastReceiver{
  2. publicfinalstaticStringACTION_SEND="pw.msdx.ACTION_SEND<spanstyle="font-family:Arial,Helvetica,sans-serif;">";</span>
  3. @Override
  4. publicvoidonReceive(finalContextcontext,Intentintent){
  5. Stringaction=intent.getAction();
  6. if(ACTION_SEND.equals(action)){
  7. Log.i("SendReceiver","sendamessage");
  8. }
  9. }
  10. }

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

[html] view plain copy
  1. <receiver
  2. android:name=".SendReceiver"
  3. android:enabled="true"
  4. android:exported="true"
  5. >
  6. <intent-filter>
  7. <actionandroid:name="pw.msdx.ACTION_SEND"/>
  8. </intent-filter>
  9. </receiver>

更多相关文章

  1. android之lint警告This Handler class should be static or leak
  2. Android中Context和Resource之间的关系
  3. Android开发面试经——常见面试官提问Android题②
  4. Android(安卓)app接收来自adb发送的广播的实例学习
  5. Android(安卓)Content Provider在应用程序之间共享数据的原理分
  6. Android(安卓)Action的常用
  7. Android的跨进程通信
  8. android开机动画播放流程
  9. 疯狂安卓Android自学笔记

随机推荐

  1. 解决gradle DSL method not found: andro
  2. android主界面tab切换方式
  3. 阿里 Andfix 介绍及原理解析
  4. Android解析XML到对象工具类
  5. android LinearLayout添加分割线
  6. Android 学习 之 关闭应用程序的方法
  7. Android(安卓)RecyclerView聊天界面控件
  8. 分享一款android的日历组件
  9. android 系统广播
  10. Android(安卓)汉字的正则表达式