利用pendingintent 和AlarmManager实现定时任务的一些分析

PendingIntent
获取 PendingIntent对象的方法:
可以通过getActivity(Context context, int requestCode, Intent intent, int flags)系列方法从系统取得一个用于启动一个Activity的PendingIntent对象,

可以通过getService(Context context, int requestCode, Intent intent, int flags)方法从系统取得一个用于启动一个Service的PendingIntent对象

可以通过getBroadcast(Context context, int requestCode, Intent intent, int flags)方法从系统取得一个用于向BroadcastReceiver的Intent广播的PendingIntent对象
其中requestCode:Private request code for the sender
flags:

FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。
FLAG_NO_CREATE:如果当前系统中不存在相同的PendingIntent对象,系统将不会创建该PendingIntent对象而是直接返回null。
FLAG_ONE_SHOT:该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException。
FLAG_UPDATE_CURRENT:如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。
注意:
1.requestCode不同会导致获取的PendingIntent不同。
2.两个PendingIntent对等是指它们的operation一样, 且其它们的Intent的action, data, categories, components和flags都一样。但是它们的Intent的Extra可以不一样

AlarmManager
AlarmManager的闹钟类型及方法:

Android系统提供了四种类型的闹钟:

(1)、ELAPSED_REALTIME:在指定的延时之后发送Intent,但不唤醒设备

(2)、ELAPSED_REALTIME_WAKEUP:在指定的延时之后发送Intent,同时唤醒设备 延时是会把系统启动的时间SystemClock.elapsedRealtime()算进去!!

(3)、RTC:在指定的时刻发送Intent,但不唤醒设备

(4)、RTC_WAKEUP:在指定的时刻发送Intent,同时唤醒设备

AlarmManager的方法:

(1)、void set(int type, long triggerAtTime, PendingIntent operation)
注册一个闹钟

(2)、void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
注册一个会重复的闹钟

(3)、void setInexactRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
注册一个重复闹钟的不精确版本,它相对而言更节能(power-efficient)一些,因为系统可能会将几个差不多的闹钟合并为一个来执行,减少设备的唤醒次数。
内置的几个interval为:
INTERVAL_FIFTEEN_MINUTES
INTERVAL_HALF_HOUR
INTERVAL_HOUR
INTERVAL_HALF_DAY
INTERVAL_DAY
如果你将其设为DAY,那么可能这一天中的所有闹钟都会被合并掉。

(4)、void cancel(PendingIntent operation)
取消一个注册的闹钟

(5)、void setTimeZone(String timeZone)
设置系统的默认时区。需要android.permission.SET_TIME_ZONE权限

(5)、voidsetTime(long millis)

设置系统挂钟时间,需要android.permission.SET_TIME权限

在使用时:
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
Intent i = new Intent();
i.setAction(action);
i.putExtra(“key”, “value”);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0,
i, PendingIntent.FLAG_UPDATE_CURRENT);
final long time = System.currentTimeMillis();
am.setRepeating(AlarmManager.RTC_WAKEUP, time, 10 * 1000, pi);
如果想要修改Intent中PutExtra()中的值
需要重新 pi = PendingIntent.getBroadcast(getApplicationContext(), 0,
i, PendingIntent.FLAG_UPDATE_CURRENT);
重新 am.setRepeating()
自己做的实验,如果新的pi和之前的pi是同等的,在重新am.setRepeating()之前没有am.cancel(pi),没有看出来有什么影响
并且设置PendingIntent.FLAG_UPDATE_CURRENT和FLAG_CANCEL_CURRENT没有看出区别,不过不要设置成FLAG_ONE_SHOT,因为如果设置了这个,则只会执行一次,不会重复
网上介绍的比较好的文章:
http://blog.csdn.net/hudashi/article/details/7060837
http://my.oschina.net/youranhongcha/blog/196933?p=2#comments
http://www.cnblogs.com/mengdd/p/3819806.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值