Android中的AlarmManager类

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

一、声明:public class AlarmManager extends Object

二、类结构:

java.lang.Object
   ↳android.app.AlarmManager

三、概述:

该类提供一种接近系统闹钟服务的方式,允许你去设置一个将来的时间点去执行你的应用程序。当你的闹钟响起(时间到)时,在它上面注册的一个意图(Intent)将会被系统以广播发出,然后自动启动目标程序,如果它并没有正在运行。注册的闹钟会被保留即使设备处于休眠中(如果闹钟在给定时间响起可以选择是否唤醒设备)。如果闹钟关闭或者重启,闹钟将被清除。

只要广播的onReceive()方法正在执行,这闹钟管理者(AlarmManager)会持有一个CPU唤醒锁,这是为了保证手机不会休眠直到处理完该广播,一旦onReceive()返回,那么闹钟管理者将会释放唤醒锁。这意味着你的手机在广播一处理完有可能进入休眠,如果你的闹钟广播接收者调用的是Context.startService(),那么手机有可能在被请求的服务执行之前进入休眠,为了防止这种情况,你的BroadcastReceiver和服务需要实现一个单独的唤醒锁策略以确保手机继续运行,直到服务可用。

注:该类适用于你想让应用程序在将来某个指定时间点执行的情况,即使你的应用程序现在没有运行。对一般的时间操作,使用Handler是更容易和更有效率的。

自API 19以后,闹钟触发是不精确的:操作系统将会使用闹钟时间产生变化,为了减少唤醒和电池使用。有新的API支持那些需要严格闹钟触发时间的应用程序,参见:setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent).而在API 19以前的版本参照以前的处理方法,他们都是按照请求精确触发的。

四、常量:

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

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

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

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

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

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

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

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

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

五、方法:

1、public void cancel (PendingIntent operation) 根据匹配的Intent,移除闹钟。Intent封装在operation中。任何类型的闹钟,只要匹配该条件都会被取消。

2、public void set (int type, long triggerAtMillis, PendingIntent operation)   设置闹钟。

注:对常用时间操作,使用Handler更容易,如果之前设置过同一个IntentSender的闹钟,那么之前的首选会被取消。如果设置的时间在当前时间之前,那么闹钟将马上触发。

参数:type:One of ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC, or RTC_WAKEUP.

3、public void setExact (int type, long triggerAtMillis, PendingIntent operation) 按照指定时间设置闹钟,类似于set(int, long, PendingIntent),但是该方法不允许操作系统调整触发时间,闹钟会尽可能地在请求时间触发。

注:只有在要求闹钟时间非常精确的情况才使用该方法进行设置。应用程序非常不支持在不必要的情况下使用该方法,因为它会降低操作系统减少电池使用的能力。

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

设置一个重复的闹钟,它的触发时间是不精确的。该方法比setRepeating(int, long, long, PendingIntent)节省电量。因为系统能调整触发时间,避免不必要的唤醒设备。

5、public void setRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)  设置重复闹钟。

6、public void setTime (long millis)  设置系统“墙”时钟。需要android.permission.SET_TIME.权限。

7、public void setTimeZone (String timeZone)   设置系统默认时区,要求android.permission.SET_TIME_ZONE.权限。

参数:由TimeZone支持。

8、public void setWindow (int type, long windowStartMillis, long windowLengthMillis, PendingIntent operation) 设置一个闹钟在给定的时间窗触发。类似于set(int, long, PendingIntent),该方法允许应用程序精确地控制操作系统调整闹钟触发时间的程度。

参数:

windowStartMillisThe earliest time, in milliseconds, that the alarm should be delivered, expressed in the appropriate clock's units (depending on the alarm type).
windowLengthMillisThe length of the requested delivery window, in milliseconds. The alarm will be delivered no later than this many milliseconds after windowStartMillis. Note that this parameter is a duration, not the timestamp of the end of the window.

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值