AlarmManager使用心得
AlarmManager使用心得
最近在做的项目,对于性能和功耗的优化非常重视。由此,也学习到了很多AlarmManager好用的用法。
基于此,我也对AlarmManager的内容产生了一些兴趣。去了android官网上查阅了AlarmManager相关资料,对比项目上实践过的内容,加深印象。
总的来说setExactAndAllowWhileIdle 是比 setAndAllowWhileIdle 要准时。
查阅官网时,发现AlarmManager没有中文翻译。所以机翻了一下,以便阅读。
此类提供对系统警报服务的访问。 这些使您可以安排应用程序在将来的某个时间运行。
警报响起时,系统会广播已为其注册的Intent,并在目标应用程序尚未运行时自动启动它。
设备处于睡眠状态时会保留已注册的警报(如果警报在这段时间内关闭,可以选择将其唤醒),但是如果将其关闭并重新启动,则将被清除。只要警报接收器的onReceive()方法正在执行,警报管理器就会保持CPU唤醒锁。 这样可以确保手机在完成广播处理之前不会进入睡眠状态。
一旦onReceive()返回,警报管理器将释放此唤醒锁。 这意味着在某些情况下,一旦onReceive()方法完成,电话就会进入休眠状态。
如果您的警报接收器称为Context.startService(),则手机可能会在启动请求的服务之前进入睡眠状态。
为避免这种情况,您的BroadcastReceiver和服务将需要实施单独的唤醒锁定策略,以确保电话继续运行,直到服务可用为止。警报管理器适用于希望在特定时间运行应用程序代码的情况,即使您的应用程序当前未运行。
对于正常的计时操作(滴答声,超时等),使用Handler会更容易且效率更高。
从API
19(Build.VERSION_CODES.KITKAT)开始,警报传递是不精确的:操作系统将转移警报,以最大程度地减少唤醒和电池消耗。这里有新的API支持需要严格传递保证的应用;
请参见setWindow(int,long,long,android.app.PendingIntent)和setExact(int,long,android.app.PendingIntent)。
Summary Nested classes class AlarmManager.AlarmClockInfo An immutable
description of a scheduled “alarm clock” event.
interface AlarmManager.OnAlarmListener
直接通知警报:从设置警报到传递警报,请求者必须连续运行,否则传递失败.
String ACTION_NEXT_ALARM_CLOCK_CHANGED Broadcast Action: Sent after
the value returned by getNextAlarmClock() has changed.
int ELAPSED_REALTIME
SystemClock.elapsedRealtime()中的警报时间(自启动以来的时间,包括睡眠)。 此警报不会唤醒设备;
如果设备在睡眠状态下熄灭,则将在下次设备唤醒时将其交付。
int ELAPSED_REALTIME_WAKEUP
SystemClock.elapsedRealtime()中的警报时间(自启动以来的时间,包括睡眠),它将在设备关闭时将其唤醒。
long INTERVAL_DAY 在API
19之前的Android上运行时,setInexactRepeating(int,long,long,android.app.PendingIntent)可以识别的可用不精确重复间隔。
long INTERVAL_FIFTEEN_MINUTES 在API
19之前的Android上运行时,setInexactRepeating(int,long,long,android.app.PendingIntent)可以识别的可用不精确重复间隔。
long INTERVAL_HALF_DAY 在API
19之前的Android上运行时,setInexactRepeating(int,long,long,android.app.PendingIntent)可以识别的可用不精确重复间隔。
long INTERVAL_HALF_HOUR 在API
19之前的Android上运行时,setInexactRepeating(int,long,long,android.app.PendingIntent)可以识别的可用不精确重复间隔。
long INTERVAL_HOUR 在API
19之前的Android上运行时,setInexactRepeating(int,long,long,android.app.PendingIntent)可以识别的可用不精确重复间隔。
int RTC System#currentTimeMillis中的警报时间(UTC中的挂钟时间)。 此警报不会唤醒设备;
如果设备在睡眠状态下熄灭,则将在下次设备唤醒时将其交付。
int RTC_WAKEUP
System#currentTimeMillis中的警报时间(UTC中的挂钟时间),在设备关闭时将唤醒设备。
Public methods
void cancel(PendingIntent operation) Remove any alarms with a matching Intent.
void cancel(AlarmManager.OnAlarmListener listener)
Remove any alarm scheduled to be delivered to the given OnAlarmListener.
AlarmManager.AlarmClockInfo getNextAlarmClock() Gets information about
the next alarm clock currently scheduled.
void set(int type, long triggerAtMillis, PendingIntent operation)
Schedule an alarm.
void set(int type, long triggerAtMillis, String tag, AlarmManager.OnAlarmListener listener, Handler targetHandler)
Direct callback version of set(int, long, android.app.PendingIntent).
void setAlarmClock(AlarmManager.AlarmClockInfo info, PendingIntent operation)
Schedule an alarm that represents an alarm clock, which will be used to notify the user when it goes off.
void setAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation)
Like set(int, long, android.app.PendingIntent), but this alarm will be allowed to execute even when the system is in low-power idle (a.k.a. void setExact(int type, long triggerAtMillis, PendingIntent operation) Schedule an alarm to be delivered precisely at the stated time.
void setExact(int type, long triggerAtMillis, String tag, AlarmManager.OnAlarmListener listener, Handler targetHandler)
Direct callback version of setExact(int, long, android.app.PendingIntent).
void setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation)
Like setExact(int, long, android.app.PendingIntent), but this alarm will be allowed to execute even when the system is in low-power idle modes.
void setInexactRepeating(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.
void setRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
Schedule a repeating alarm.
void setTime(long millis)
Set the system wall clock time.
void setTimeZone(String timeZone)
Sets the system’s persistent default time zone.
void setWindow(int type, long windowStartMillis, long windowLengthMillis, PendingIntent operation)
Schedule an alarm to be delivered within a given window of time.
void setWindow(int type, long windowStartMillis, long windowLengthMillis, String tag, AlarmManager.OnAlarmListener listener, Handler targetHandler)
Direct callback version of setWindow(int, long, long, android.app.PendingIntent). Inherited methods From class java.lang.Object Constants Public methods cancel Added in API level 1
public void cancel (PendingIntent operation)
Remove any alarms with a matching Intent. Any alarm, of any type, whose Intent matches this one (as defined by Intent#filterEquals), will be canceled. Parameters operation PendingIntent: IntentSender which matches a previously added IntentSender. This parameter must not be null. See also: set(int, long, PendingIntent) cancel Added in API level 24
public void cancel (AlarmManager.OnAlarmListener listener)
Remove any alarm scheduled to be delivered to the given OnAlarmListener. Parameters listener AlarmManager.OnAlarmListener: OnAlarmListener instance that is the target of a currently-set alarm.
getNextAlarmClock Added in API level 21 public
AlarmManager.AlarmClockInfo getNextAlarmClock () 获取有关当前计划的下一个闹钟的信息。
所考虑的闹钟是任何应用程序使用setAlarmClock(AlarmManager.AlarmClockInfo,PendingIntent)方法安排的闹钟。
Returns AlarmManager.AlarmClockInfo An AlarmClockInfo object
describing the next upcoming alarm clock event that will occur. If
there are no alarm clock events currently scheduled, this method will
return null. See also: setAlarmClock(AlarmManager.AlarmClockInfo,
PendingIntent) AlarmManager.AlarmClockInfo
ACTION_NEXT_ALARM_CLOCK_CHANGED set Added in API level 1 public void
set (int type,
long triggerAtMillis,
PendingIntent operation) Schedule an alarm. Note: for timing operations (ticks, timeouts, etc) it is easier and much more
efficient to use Handler. If there is already an alarm scheduled for
the same IntentSender, that previous alarm will first be canceled. If
the stated trigger time is in the past, the alarm will be triggered
immediately. If there is already an alarm for this Intent scheduled
(with the equality of two intents being defined by
Intent#filterEquals), then it will be removed and replaced by this
one. The alarm is an Intent broadcast that goes to a broadcast
receiver that you registered with
Context.registerReceiver(BroadcastReceiver, IntentFilter) or through
the tag in an AndroidManifest.xml file.
警报意图与额外的int类型数据(称为Intent#EXTRA_ALARM_COUNT)一起传递,该数据指示此意图广播中已累积了多少过去的警报事件。
由于电话处于睡眠状态而未发送的重复警报可能在发送时计数大于一。
set Added in API level 24 public void set (int type,
long triggerAtMillis,
String tag,
AlarmManager.OnAlarmListener listener,
Handler targetHandler) Direct callback version of set(int, long, android.app.PendingIntent). Rather than supplying a
PendingIntent to be sent when the alarm time is reached, this variant
supplies an OnAlarmListener instance that will be invoked at that
time. The OnAlarmListener’s OnAlarmListener#onAlarm() method will be
invoked via the specified target Handler, or on the application’s main
looper if null is passed as the targetHandler parameter.
set(int,long,android.app.PendingIntent)的直接回调版本。
该变量提供了一个OnAlarmListener实例,该实例将在该时间调用,而不是提供到达警报时间时要发送的PendingIntent。
OnAlarmListener的OnAlarmListener#onAlarm()方法将通过指定的目标处理程序调用,或者如果通过null作为targetHandler参数传递,则在应用程序的主循环程序上调用。
setAlarmClock Added in API level 21 public void setAlarmClock
(AlarmManager.AlarmClockInfo info,
PendingIntent operation) Schedule an alarm that represents an alarm clock, which will be used to notify the user when
it goes off. The expectation is that when this alarm triggers, the
application will further wake up the device to tell the user about the
alarm – turning on the screen, playing a sound, vibrating, etc. As
such, the system will typically also use the information supplied here
to tell the user about this upcoming alarm if appropriate. Due to the
nature of this kind of alarm, similar to
setExactAndAllowWhileIdle(int, long, PendingIntent), these alarms will
be allowed to trigger even if the system is in a low-power idle
(a.k.a. doze) mode. The system may also do some prep-work when it sees
that such an alarm coming up, to reduce the amount of background work
that could happen if this causes the device to fully wake up – this
is to avoid situations such as a large number of devices having an
alarm set at the same time in the morning, all waking up at that time
and suddenly swamping the network with pending background work. As
such, these types of alarms can be extremely expensive on battery use
and should only be used for their intended purpose. This method is
like setExact(int, long, android.app.PendingIntent), but implies
RTC_WAKEUP.计划代表闹钟的闹钟,闹钟将在关闭时通知用户。期望当该警报触发时,应用程序将进一步唤醒设备以告知用户有关警报的信息-打开屏幕,播放声音,振动等。因此,系统通常也会使用该信息如果需要,可在此处提供此信息以告知用户即将发生的警报。
由于这种警报的性质,类似于setExactAndAllowWhileIdle(int,long,PendingIntent),即使系统处于低功率空闲(也称为打ze睡)模式,也将允许触发这些警报。当系统看到此类警报时,系统可能还会做一些准备工作,以减少可能导致设备完全唤醒的后台工作量–以避免发生大量此类情况早上在同一时间设置了警报的设备中,所有设备都在该时间醒来,并突然有待处理的后台工作淹没了网络。因此,这些类型的警报在电池使用上可能非常昂贵,并且仅应用于其预期目的。
setAndAllowWhileIdle Added in API level 23 public void
setAndAllowWhileIdle (int type,
long triggerAtMillis,
PendingIntent operation) Like set(int, long, android.app.PendingIntent), but this alarm will be allowed to execute
even when the system is in low-power idle (a.k.a. doze) modes. This
type of alarm must only be used for situations where it is actually
required that the alarm go off while in idle – a reasonable example
would be for a calendar notification that should make a sound so the
user is aware of it. When the alarm is dispatched, the app will also
be added to the system’s temporary whitelist for approximately 10
seconds to allow that application to acquire further wake locks in
which to complete its work. These alarms can significantly impact the
power use of the device when idle (and thus cause significant battery
blame to the app scheduling them), so they should be used with care.
To reduce abuse, there are restrictions on how frequently these alarms
will go off for a particular application. Under normal system
operation, it will not dispatch these alarms more than about every
minute (at which point every such pending alarm is dispatched); when
in low-power idle modes this duration may be significantly longer,
such as 15 minutes. Unlike other alarms, the system is free to
reschedule this type of alarm to happen out of order with any other
alarms, even those from the same app. This will clearly happen when
the device is idle (since this alarm can go off while idle, when any
other alarms from the app will be held until later), but may also
happen even when not idle. Regardless of the app’s target SDK version,
this call always allows batching of the alarm.类似于set(int,long,android.app.PendingIntent),但是即使系统处于低功耗空闲(也称为打ze睡)模式,也将允许执行此警报。此类警报只能用于实际需要在闲置状态下发出警报的情况-一个合理的例子是日历通知应发出声音,以便用户知道。发出警报后,该应用程序还将被添加到系统的临时白名单中大约10秒钟,以使该应用程序获得更多唤醒锁以完成其工作。
这些警报在闲置时会严重影响设备的电源使用(并因此严重影响了安排它们的应用程序的电池消耗),因此应谨慎使用。为了减少滥用,对特定应用程序发出这些警报的频率有限制。在正常的系统操作下,它将不超过大约每分钟分发这些警报(此时将分发每个此类挂起的警报)。在低功率空闲模式下,此持续时间可能会明显更长,例如15分钟。
与其他警报不同,系统可以*地重新安排此类警报的发生时间,使其与其他任何警报(即使来自同一应用程序的警报)也可以不按顺序进行。当设备闲置时,显然会发生这种情况(因为该警报可以在闲置时发出,而应用中的任何其他警报将保留到以后),但即使在闲置时也可能发生。
无论应用程序的目标SDK版本是什么,此调用始终允许警报的批处理。
setExactAndAllowWhileIdle Added in API level 23 public void
setExactAndAllowWhileIdle (int type,
long triggerAtMillis,
PendingIntent operation) Like setExact(int, long, android.app.PendingIntent), but this alarm will be allowed to execute
even when the system is in low-power idle modes. If you don’t need
exact scheduling of the alarm but still need to execute while idle,
consider using setAndAllowWhileIdle(int, long, PendingIntent). This
type of alarm must only be used for situations where it is actually
required that the alarm go off while in idle – a reasonable example
would be for a calendar notification that should make a sound so the
user is aware of it. When the alarm is dispatched, the app will also
be added to the system’s temporary whitelist for approximately 10
seconds to allow that application to acquire further wake locks in
which to complete its work. These alarms can significantly impact the
power use of the device when idle (and thus cause significant battery
blame to the app scheduling them), so they should be used with care.
To reduce abuse, there are restrictions on how frequently these alarms
will go off for a particular application. Under normal system
operation, it will not dispatch these alarms more than about every
minute (at which point every such pending alarm is dispatched); when
in low-power idle modes this duration may be significantly longer,
such as 15 minutes. Unlike other alarms, the system is free to
reschedule this type of alarm to happen out of order with any other
alarms, even those from the same app. This will clearly happen when
the device is idle (since this alarm can go off while idle, when any
other alarms from the app will be held until later), but may also
happen even when not idle. Note that the OS will allow itself more
flexibility for scheduling these alarms than regular exact alarms,
since the application has opted into this behavior. When the device is
idle it may take even more liberties with scheduling in order to
optimize for battery life.
类似于set Exact(int,long,android.app.PendingIntent),但即使系统处于低功耗空闲模式,也将允许执行此警报。如果不需要精确的警报调度,但仍需要在空闲时执行,请考虑使用setAndAllowWhileIdle(int,long,PendingIntent)。此类警报只能用于实际需要在空闲状态下发出警报的情况-一个合理的例子是日历通知应发出声音,以便用户注意。发出警报后,该应用程序还将被添加到系统的临时白名单中大约10秒钟,以使该应用程序获得更多唤醒锁以完成其工作。这些警报在闲置时会严重影响设备的电源使用(并因此严重影响了安排它们的应用程序的电池消耗),因此应谨慎使用。为了减少滥用,对特定应用程序发出这些警报的频率有限制。在正常的系统操作下,它将不超过大约每分钟分发这些警报(此时将分发每个此类挂起的警报)。在低功率空闲模式下,此持续时间可能会明显更长,例如15分钟。
与其他警报不同,系统可以*地重新安排此类警报的发生时间,使其与其他任何警报(即使来自同一应用程序的警报)也可以不按顺序进行。当设备闲置时,显然会发生这种情况(因为该警报可以在闲置时发出,而应用中的任何其他警报将保留到以后),但即使在闲置时也可能发生。请注意,由于应用程序选择了此行为,因此操作系统将比常规的完全警报具有更大的灵活性来安排这些警报。当设备空闲时,可能需要更多的调度时间来优化电池寿命。
本文地址:https://blog.csdn.net/ft1223ccc/article/details/107454495