展会信息港展会大全

Android中的AlarmManager类
来源:互联网   发布日期:2016-01-13 21:41:26   浏览:2585次  

导读:一、声明:public class AlarmManager extends Object 二、类结构: java.lang.Object ? android.app.AlarmManager 三、概述: 该类提供一种接近系统闹钟服务的方式,允许你去设置一个将来的时间点去执行你的应用程序。当你的闹钟响起(时间到)时,在它上面...

一、声明: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),该方法允许应用程序精确地控制操作系统调整闹钟触发时间的程度。

参数:

windowStartMillis

The earliest time, in milliseconds, that the alarm should be delivered, expressed in the appropriate clock's units (depending on the alarm type).

windowLengthMillis

The 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.

赞助本站

人工智能实验室

相关热词: android开发

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港