关于AlarmManager的setRepeat使用注意事项

来源:互联网 发布:淘宝如何举报店铺 编辑:程序博客网 时间:2024/04/29 09:31

AlarmManager顾名思义,闹钟管理器,即为你将要到来的某动作设置一个定时闹钟,当到达触发时间的时候(可以理解为闹钟响的时候),来触发一个任务,这个任务是一个pendingIntent。


首先我们先来看一下android API里边关于AlarmManager(点击)的描述:

This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

意思大概和上面说的一个意思,但要注意的是,当我们的应用程序未启动时,如果之前设置过闹钟,系统会启动我们的应用,而且设置休眠时依然会保留,我们可以选择性唤醒设备,但是关机或者重启之后就会失效


接下看AlarmManager几个闹钟类型:

public static final int ELAPSED_REALTIME 该闹钟时间以SystemClock.elapsedRealtime()定义。闹钟不会唤醒设备。如果在系统休眠时闹钟触发,它将不会被传递,直到下一次设备唤醒。常量值为3。

public static final int ELAPSED_REALTIME_WAKEUP 该闹钟时间以SystemClock.elapsedRealtime()定义。当闹钟触发时会唤醒设备。常量值为2。

public static final long INTERVAL_DAY 可用的不精确的复发间隔。通过setInexactRepeating(int, long, long, PendingIntent)去辨别。运行在API 19或者之前。常量值:86400000

public static final long INTERVAL_FIFTEEN_MINUTES 可用的不精确的复发间隔。通过setInexactRepeating(int, long, long, PendingIntent)去辨别。运行在API 19或者之前。常量值:900000

public static final long INTERVAL_HALF_DAY 可用的不精确的复发间隔。通过setInexactRepeating(int, long, long, PendingIntent)去辨别。运行在API 19或者之前。常量值: 43200000

public static final long INTERVAL_HALF_HOUR 可用的不精确的复发间隔。通过setInexactRepeating(int, long, long, PendingIntent)去辨别。运行在API 19或者之前。常量值: 1800000

public static final long INTERVAL_HOUR 可用的不精确的复发间隔。通过setInexactRepeating(int, long, long, PendingIntent)去辨别。运行在API 19或者之前。常量值: 3600000

public static final int RTC 该时间以System.currentTimeMillis()定义。闹钟不会唤醒设备。如果在系统休眠时闹钟触发,它将不会被传递,直到下一次设备唤醒。常量值为1。

public static final int RTC_WAKEUP 该时间以System.currentTimeMillis()定义。当闹钟触发时会唤醒设备。常量值为0。


一般最常用的闹钟类型为RTC,以下的讲解都是基于RTC


接下来我们来看AlarmManager最重要的3个方法:

voidcancel(PendingIntent operation)

Remove any alarms with a matching Intent.
voidset(int type, long triggerAtMillis, PendingIntent operation)

Schedule an alarm.

voidsetExact(int type, long triggerAtMillis, PendingIntent operation)
Schedule an alarm to be delivered precisely at the stated time.
voidsetInexactRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
Schedule a repeating alarm that has inexact trigger time requirements; for example, an alarm that repeats every hour, but not necessarily at the top of every hour.
voidsetRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
Schedule a repeating alarm.
接下来我们就一个一个地来看:

void cancel(PendingIntent operation)

根据Intent的内容,移除闹钟,任何类型的闹钟,只要匹配上,都会被移除


voidset(int type, long triggerAtMillis, PendingIntent operation)

设置一个闹钟,三个参数分别为类型、触发时间和一个封装Intent的PendingIntent,这个闹钟会在我们设定的时间去触发,如果先设置了触发时间,再调

系统时间到触发时间之后,则调完时间,闹钟会马上被触发。


void setExact(int type, long triggerAtMillis, PendingIntent operation)

与set方法类似,不过设置的是精确时间,会在精确的时间点触发,如果调了系统实际超过了触发时间,则闹钟不会被触发


void setRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)

设置一个重复的闹钟,三个参数分别为类型、触发时间、重复周期和一个封装Intent的PendingIntent,这个闹钟会在我们设定的时间去触发,然后

在根据重复周期去设置下一个触发时间,这里需要注意的是,在设置下一个时间的时候,是根据第一个时间加上N个时间周期,即设置一个时间为0时触发

周期为8小时,则触发一定为0时、8时、16时、24时,即使调了系统时间,也不会因为上一个触发时间改变下一个触发时间,即在8时的时候把时间调到20时

此时会触发一次,再把时间调到24小时,此时还会触发一次。如果把时间更改为几天之后,那么系统会根据到首次触发的时间去计算有多少次没有被触发,

会在后来的触发任务时追加上,因此可能出现一个时间周期多次触发的现象


void setInexactRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)

与setRepeating类似,不过设置的是一个非精确地闹钟,即如果触发时间为0时、8时、16时、24时,可能会在0:20,8:05,16:33等触发时间的任意时间段内触发


注意事项:

在开发的时候,如果有需要用AlarmManager设置定时任务的时候,先弄清楚需不需要准确地没隔8小时触发一次,如果需要触发时间之间相距8小时,则

推荐多次调用set方法,即上一次任务执行完的时候,再写一个回调方法去set下一个触发时间。


OK,就写到这里,转载请标明出处,谢谢。


0 0