public class AlarmManager extends Object
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.
The Alarm Manager holds a CPU wake lock as long as the alarm receiver's
onReceive() method is executing. This guarantees that the phone will not sleep
until you have finished handling the broadcast. Once onReceive() returns, the
Alarm Manager releases this wake lock. This means that the phone will in some
cases sleep as soon as your onReceive() method completes. If your alarm receiver
called Context.startService()
, it
is possible that the phone will sleep before the requested service is launched.
To prevent this, your BroadcastReceiver and Service will need to implement a
separate wake lock policy to ensure that the phone continues running until the
service becomes available.
Note: The Alarm Manager is intended for cases where you want to have
your application code run at a specific time, even if your application is
not currently running. For normal timing operations (ticks, timeouts,
etc) it is easier and much more efficient to use
Handler
.
Note: Beginning with API 19
(Build.VERSION_CODES.KITKAT
) alarm delivery is inexact:
the OS will shift alarms in order to minimize wakeups and battery use. There are
new APIs to support applications which need strict delivery guarantees; see
setWindow(int, long, long, PendingIntent)
and
setExact(int, long, PendingIntent)
. Applications whose targetSdkVersion
is earlier than API 19 will continue to see the previous behavior in which all
alarms are delivered exactly when requested.
You do not
instantiate this class directly; instead, retrieve it through
Context.getSystemService(Context.ALARM_SERVICE)
.
Modifier and Type | Class and Description |
---|---|
static class |
AlarmManager.AlarmClockInfo
An immutable description of a scheduled "alarm clock" event.
|
static interface |
AlarmManager.OnAlarmListener
Direct-notification alarms: the requester must be running continuously from the
time the alarm is set to the time it is delivered, or delivery will fail.
|
Modifier and Type | Field and Description |
---|---|
static String |
ACTION_NEXT_ALARM_CLOCK_CHANGED
Broadcast Action: Sent after the value returned by
getNextAlarmClock() has changed. |
static int |
ELAPSED_REALTIME
Alarm time in
SystemClock.elapsedRealtime() (time since boot, including sleep). |
static int |
ELAPSED_REALTIME_WAKEUP
Alarm time in
SystemClock.elapsedRealtime() (time since boot, including sleep),
which will wake up the device when it goes off. |
static int |
FLAG_ALLOW_WHILE_IDLE
Flag for alarms: this alarm would like to still execute even if the device is
idle.
|
static int |
FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED
Flag for alarms: same as
FLAG_ALLOW_WHILE_IDLE , but doesn't have restrictions
on how frequently it can be scheduled. |
static int |
FLAG_IDLE_UNTIL
Flag for alarms: this alarm marks the point where we would like to come out of idle
mode.
|
static int |
FLAG_STANDALONE
Flag for alarms: this is to be a stand-alone alarm, that should not be batched with
other alarms.
|
static int |
FLAG_WAKE_FROM_IDLE
Flag for alarms: this alarm would like to wake the device even if it is idle.
|
static long |
INTERVAL_DAY
Available inexact recurrence interval recognized by
setInexactRepeating(int, long, long, PendingIntent)
when running on Android prior to API 19. |
static long |
INTERVAL_FIFTEEN_MINUTES
Available inexact recurrence interval recognized by
setInexactRepeating(int, long, long, PendingIntent)
when running on Android prior to API 19. |
static long |
INTERVAL_HALF_DAY
Available inexact recurrence interval recognized by
setInexactRepeating(int, long, long, PendingIntent)
when running on Android prior to API 19. |
static long |
INTERVAL_HALF_HOUR
Available inexact recurrence interval recognized by
setInexactRepeating(int, long, long, PendingIntent)
when running on Android prior to API 19. |
static long |
INTERVAL_HOUR
Available inexact recurrence interval recognized by
setInexactRepeating(int, long, long, PendingIntent)
when running on Android prior to API 19. |
static int |
RTC
Alarm time in
System.currentTimeMillis()
(wall clock time in UTC). |
static int |
RTC_WAKEUP
Alarm time in
System.currentTimeMillis()
(wall clock time in UTC), which will wake up the device when
it goes off. |
static long |
WINDOW_EXACT |
static long |
WINDOW_HEURISTIC |
Modifier and Type | Method and Description |
---|---|
void |
cancel(AlarmManager.OnAlarmListener listener)
Remove any alarm scheduled to be delivered to the given
AlarmManager.OnAlarmListener . |
void |
cancel(PendingIntent operation)
Remove any alarms with a matching
Intent . |
AlarmManager.AlarmClockInfo |
getNextAlarmClock()
Gets information about the next alarm clock currently scheduled.
|
AlarmManager.AlarmClockInfo |
getNextAlarmClock(int userId)
Gets information about the next alarm clock currently scheduled.
|
long |
getNextWakeFromIdleTime() |
void |
set(int type,
long triggerAtMillis,
long windowMillis,
long intervalMillis,
AlarmManager.OnAlarmListener listener,
Handler targetHandler,
WorkSource workSource)
Direct callback version of
set(int, long, long, long, PendingIntent, WorkSource) . |
void |
set(int type,
long triggerAtMillis,
long windowMillis,
long intervalMillis,
PendingIntent operation,
WorkSource workSource) |
void |
set(int type,
long triggerAtMillis,
long windowMillis,
long intervalMillis,
String tag,
AlarmManager.OnAlarmListener listener,
Handler targetHandler,
WorkSource workSource)
Direct callback version of
set(int, long, long, long, PendingIntent, WorkSource) . |
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, PendingIntent) . |
void |
setAlarmClock(AlarmManager.AlarmClockInfo info,
PendingIntent operation)
Schedule an alarm that represents an alarm clock.
|
void |
setAndAllowWhileIdle(int type,
long triggerAtMillis,
PendingIntent operation)
Like
set(int, long, PendingIntent) , but this alarm will be allowed to execute
even when the system is in low-power idle modes. |
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, PendingIntent) . |
void |
setExactAndAllowWhileIdle(int type,
long triggerAtMillis,
PendingIntent operation)
Like
setExact(int, long, PendingIntent) , but this alarm will be allowed to execute
even when the system is in low-power idle modes. |
void |
setIdleUntil(int type,
long triggerAtMillis,
String tag,
AlarmManager.OnAlarmListener listener,
Handler targetHandler)
Schedule an idle-until alarm, which will keep the alarm manager idle until
the given time.
|
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, PendingIntent) . |
public static final int RTC_WAKEUP
System.currentTimeMillis()
(wall clock time in UTC), which will wake up the device when
it goes off.public static final int RTC
System.currentTimeMillis()
(wall clock time in UTC). This alarm does not wake the
device up; if it goes off while the device is asleep, it will not be
delivered until the next time the device wakes up.public static final int ELAPSED_REALTIME_WAKEUP
SystemClock.elapsedRealtime()
(time since boot, including sleep),
which will wake up the device when it goes off.public static final int ELAPSED_REALTIME
SystemClock.elapsedRealtime()
(time since boot, including sleep).
This alarm does not wake the device up; if it goes off while the device
is asleep, it will not be delivered until the next time the device
wakes up.public static final String ACTION_NEXT_ALARM_CLOCK_CHANGED
getNextAlarmClock()
has changed.
This is a protected intent that can only be sent by the system. It is only sent to registered receivers.
public static final long WINDOW_EXACT
public static final long WINDOW_HEURISTIC
public static final int FLAG_STANDALONE
public static final int FLAG_WAKE_FROM_IDLE
public static final int FLAG_ALLOW_WHILE_IDLE
public static final int FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED
FLAG_ALLOW_WHILE_IDLE
, but doesn't have restrictions
on how frequently it can be scheduled. Only available (and automatically applied) to
system alarms.public static final int FLAG_IDLE_UNTIL
public static final long INTERVAL_FIFTEEN_MINUTES
setInexactRepeating(int, long, long, PendingIntent)
when running on Android prior to API 19.public static final long INTERVAL_HALF_HOUR
setInexactRepeating(int, long, long, PendingIntent)
when running on Android prior to API 19.public static final long INTERVAL_HOUR
setInexactRepeating(int, long, long, PendingIntent)
when running on Android prior to API 19.public static final long INTERVAL_HALF_DAY
setInexactRepeating(int, long, long, PendingIntent)
when running on Android prior to API 19.public static final long INTERVAL_DAY
setInexactRepeating(int, long, long, PendingIntent)
when running on Android prior to API 19.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(android.content.Intent)
), 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(android.content.BroadcastReceiver, android.content.IntentFilter)
or through the <receiver> tag in an AndroidManifest.xml file.
Alarm intents are delivered with a data extra of type int called
Intent.EXTRA_ALARM_COUNT
that indicates
how many past alarm events have been accumulated into this intent
broadcast. Recurring alarms that have gone undelivered because the
phone was asleep may have a count greater than one when delivered.
Note: Beginning in API 19, the trigger time passed to this method is treated as inexact: the alarm will not be delivered before this time, but may be deferred and delivered some time later. The OS will use this policy in order to "batch" alarms together across the entire system, minimizing the number of times the device needs to "wake up" and minimizing battery use. In general, alarms scheduled in the near future will not be deferred as long as alarms scheduled far in the future.
With the new batching policy, delivery ordering guarantees are not as
strong as they were previously. If the application sets multiple alarms,
it is possible that these alarms' actual delivery ordering may not match
the order of their requested delivery times. If your application has
strong ordering requirements there are other APIs that you can use to get
the necessary behavior; see setWindow(int, long, long, PendingIntent)
and setExact(int, long, PendingIntent)
.
Applications whose targetSdkVersion
is before API 19 will
continue to get the previous alarm behavior: all of their scheduled alarms
will be treated as exact.
type
- One of ELAPSED_REALTIME
, ELAPSED_REALTIME_WAKEUP
,
RTC
, or RTC_WAKEUP
.triggerAtMillis
- time in milliseconds that the alarm should go
off, using the appropriate clock (depending on the alarm type).operation
- Action to perform when the alarm goes off;
typically comes from IntentSender.getBroadcast()
.Handler
,
setExact(int, long, android.app.PendingIntent)
,
setRepeating(int, long, long, android.app.PendingIntent)
,
setWindow(int, long, long, android.app.PendingIntent)
,
cancel(android.app.PendingIntent)
,
Context.sendBroadcast(android.content.Intent)
,
Context.registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)
,
Intent.filterEquals(android.content.Intent)
,
ELAPSED_REALTIME
,
ELAPSED_REALTIME_WAKEUP
,
RTC
,
RTC_WAKEUP
public void set(int type, long triggerAtMillis, String tag, AlarmManager.OnAlarmListener listener, Handler targetHandler)
set(int, long, PendingIntent)
. Rather than
supplying a PendingIntent to be sent when the alarm time is reached, this variant
supplies an AlarmManager.OnAlarmListener
instance that will be invoked at that time.
The OnAlarmListener's 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.
type
- One of ELAPSED_REALTIME
, ELAPSED_REALTIME_WAKEUP
,
RTC
, or RTC_WAKEUP
.triggerAtMillis
- time in milliseconds that the alarm should go
off, using the appropriate clock (depending on the alarm type).tag
- string describing the alarm, used for logging and battery-use
attributionlistener
- AlarmManager.OnAlarmListener
instance whose
onAlarm()
method will be
called when the alarm time is reached. A given OnAlarmListener instance can
only be the target of a single pending alarm, just as a given PendingIntent
can only be used with one alarm at a time.targetHandler
- Handler
on which to execute the listener's onAlarm()
callback, or null
to run that callback on the main looper.public void setRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
Handler
. If there is already an alarm scheduled
for the same IntentSender, it will first be canceled.
Like set(int, long, android.app.PendingIntent)
, except you can also supply a period at which
the alarm will automatically repeat. This alarm continues
repeating until explicitly removed with cancel(android.app.PendingIntent)
. If the stated
trigger time is in the past, the alarm will be triggered immediately, with an
alarm count depending on how far in the past the trigger time is relative
to the repeat interval.
If an alarm is delayed (by system sleep, for example, for non _WAKEUP alarm types), a skipped repeat will be delivered as soon as possible. After that, future alarms will be delivered according to the original schedule; they do not drift over time. For example, if you have set a recurring alarm for the top of every hour but the phone was asleep from 7:45 until 8:45, an alarm will be sent as soon as the phone awakens, then the next alarm will be sent at 9:00.
If your application wants to allow the delivery times to drift in order to guarantee that at least a certain time interval always elapses between alarms, then the approach to take is to use one-time alarms, scheduling the next one yourself when handling each alarm delivery.
Note: as of API 19, all repeating alarms are inexact. If your
application needs precise delivery times then it must use one-time
exact alarms, rescheduling each time as described above. Legacy applications
whose targetSdkVersion
is earlier than API 19 will continue to have all
of their alarms, including repeating alarms, treated as exact.
type
- One of ELAPSED_REALTIME
, ELAPSED_REALTIME_WAKEUP
,
RTC
, or RTC_WAKEUP
.triggerAtMillis
- time in milliseconds that the alarm should first
go off, using the appropriate clock (depending on the alarm type).intervalMillis
- interval in milliseconds between subsequent repeats
of the alarm.operation
- Action to perform when the alarm goes off;
typically comes from IntentSender.getBroadcast()
.Handler
,
set(int, long, android.app.PendingIntent)
,
setExact(int, long, android.app.PendingIntent)
,
setWindow(int, long, long, android.app.PendingIntent)
,
cancel(android.app.PendingIntent)
,
Context.sendBroadcast(android.content.Intent)
,
Context.registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)
,
Intent.filterEquals(android.content.Intent)
,
ELAPSED_REALTIME
,
ELAPSED_REALTIME_WAKEUP
,
RTC
,
RTC_WAKEUP
public void setWindow(int type, long windowStartMillis, long windowLengthMillis, PendingIntent operation)
set(int, long, PendingIntent)
, but allows the
application to precisely control the degree to which its delivery might be
adjusted by the OS. This method allows an application to take advantage of the
battery optimizations that arise from delivery batching even when it has
modest timeliness requirements for its alarms.
This method can also be used to achieve strict ordering guarantees among multiple alarms by ensuring that the windows requested for each alarm do not intersect.
When precise delivery is not required, applications should use the standard
set(int, long, PendingIntent)
method. This will give the OS the most
flexibility to minimize wakeups and battery use. For alarms that must be delivered
at precisely-specified times with no acceptable variation, applications can use
setExact(int, long, PendingIntent)
.
type
- One of ELAPSED_REALTIME
, ELAPSED_REALTIME_WAKEUP
,
RTC
, or RTC_WAKEUP
.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.operation
- Action to perform when the alarm goes off;
typically comes from IntentSender.getBroadcast()
.set(int, long, android.app.PendingIntent)
,
setExact(int, long, android.app.PendingIntent)
,
setRepeating(int, long, long, android.app.PendingIntent)
,
cancel(android.app.PendingIntent)
,
Context.sendBroadcast(android.content.Intent)
,
Context.registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)
,
Intent.filterEquals(android.content.Intent)
,
ELAPSED_REALTIME
,
ELAPSED_REALTIME_WAKEUP
,
RTC
,
RTC_WAKEUP
public void setWindow(int type, long windowStartMillis, long windowLengthMillis, String tag, AlarmManager.OnAlarmListener listener, Handler targetHandler)
setWindow(int, long, long, PendingIntent)
. Rather
than supplying a PendingIntent to be sent when the alarm time is reached, this variant
supplies an AlarmManager.OnAlarmListener
instance that will be invoked at that time.
The 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.
public void setExact(int type, long triggerAtMillis, PendingIntent operation)
This method is like set(int, long, PendingIntent)
, but does not permit
the OS to adjust the delivery time. The alarm will be delivered as nearly as
possible to the requested trigger time.
Note: only alarms for which there is a strong demand for exact-time delivery (such as an alarm clock ringing at the requested time) should be scheduled as exact. Applications are strongly discouraged from using exact alarms unnecessarily as they reduce the OS's ability to minimize battery use.
type
- One of ELAPSED_REALTIME
, ELAPSED_REALTIME_WAKEUP
,
RTC
, or RTC_WAKEUP
.triggerAtMillis
- time in milliseconds that the alarm should go
off, using the appropriate clock (depending on the alarm type).operation
- Action to perform when the alarm goes off;
typically comes from IntentSender.getBroadcast()
.set(int, long, android.app.PendingIntent)
,
setRepeating(int, long, long, android.app.PendingIntent)
,
setWindow(int, long, long, android.app.PendingIntent)
,
cancel(android.app.PendingIntent)
,
Context.sendBroadcast(android.content.Intent)
,
Context.registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)
,
Intent.filterEquals(android.content.Intent)
,
ELAPSED_REALTIME
,
ELAPSED_REALTIME_WAKEUP
,
RTC
,
RTC_WAKEUP
public void setExact(int type, long triggerAtMillis, String tag, AlarmManager.OnAlarmListener listener, Handler targetHandler)
setExact(int, long, PendingIntent)
. Rather
than supplying a PendingIntent to be sent when the alarm time is reached, this variant
supplies an AlarmManager.OnAlarmListener
instance that will be invoked at that time.
The OnAlarmListener's 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.
public void setIdleUntil(int type, long triggerAtMillis, String tag, AlarmManager.OnAlarmListener listener, Handler targetHandler)
public void setAlarmClock(AlarmManager.AlarmClockInfo info, PendingIntent operation)
This method is like setExact(int, long, PendingIntent)
, but implies
RTC_WAKEUP
.
info
- operation
- Action to perform when the alarm goes off;
typically comes from IntentSender.getBroadcast()
.set(int, long, android.app.PendingIntent)
,
setRepeating(int, long, long, android.app.PendingIntent)
,
setWindow(int, long, long, android.app.PendingIntent)
,
setExact(int, long, android.app.PendingIntent)
,
cancel(android.app.PendingIntent)
,
getNextAlarmClock()
,
Context.sendBroadcast(android.content.Intent)
,
Context.registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)
,
Intent.filterEquals(android.content.Intent)
public void set(int type, long triggerAtMillis, long windowMillis, long intervalMillis, PendingIntent operation, WorkSource workSource)
public void set(int type, long triggerAtMillis, long windowMillis, long intervalMillis, String tag, AlarmManager.OnAlarmListener listener, Handler targetHandler, WorkSource workSource)
set(int, long, long, long, PendingIntent, WorkSource)
.
Note that repeating alarms must use the PendingIntent variant, not an OnAlarmListener.
The OnAlarmListener's 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.
public void set(int type, long triggerAtMillis, long windowMillis, long intervalMillis, AlarmManager.OnAlarmListener listener, Handler targetHandler, WorkSource workSource)
set(int, long, long, long, PendingIntent, WorkSource)
.
Note that repeating alarms must use the PendingIntent variant, not an OnAlarmListener.
The OnAlarmListener's 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.
public void setInexactRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
setRepeating(int, long, long, android.app.PendingIntent)
, since the
system can adjust alarms' delivery times to cause them to fire simultaneously,
avoiding waking the device from sleep more than necessary.
Your alarm's first trigger will not be before the requested time,
but it might not occur for almost a full interval after that time. In
addition, while the overall period of the repeating alarm will be as
requested, the time between any two successive firings of the alarm
may vary. If your application demands very low jitter, use
one-shot alarms with an appropriate window instead; see setWindow(int, long, long, PendingIntent)
and
setExact(int, long, PendingIntent)
.
As of API 19, all repeating alarms are inexact. Because this method has been available since API 3, your application can safely call it and be assured that it will get similar behavior on both current and older versions of Android.
type
- One of ELAPSED_REALTIME
, ELAPSED_REALTIME_WAKEUP
,
RTC
, or RTC_WAKEUP
.triggerAtMillis
- time in milliseconds that the alarm should first
go off, using the appropriate clock (depending on the alarm type). This
is inexact: the alarm will not fire before this time, but there may be a
delay of almost an entire alarm interval before the first invocation of
the alarm.intervalMillis
- interval in milliseconds between subsequent repeats
of the alarm. Prior to API 19, if this is one of INTERVAL_FIFTEEN_MINUTES,
INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_HALF_DAY, or INTERVAL_DAY
then the alarm will be phase-aligned with other alarms to reduce the
number of wakeups. Otherwise, the alarm will be set as though the
application had called setRepeating(int, long, long, android.app.PendingIntent)
. As of API 19, all repeating
alarms will be inexact and subject to batching with other alarms regardless
of their stated repeat interval.operation
- Action to perform when the alarm goes off;
typically comes from IntentSender.getBroadcast()
.Handler
,
set(int, long, android.app.PendingIntent)
,
cancel(android.app.PendingIntent)
,
Context.sendBroadcast(android.content.Intent)
,
Context.registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)
,
Intent.filterEquals(android.content.Intent)
,
ELAPSED_REALTIME
,
ELAPSED_REALTIME_WAKEUP
,
RTC
,
RTC_WAKEUP
,
INTERVAL_FIFTEEN_MINUTES
,
INTERVAL_HALF_HOUR
,
INTERVAL_HOUR
,
INTERVAL_HALF_DAY
,
INTERVAL_DAY
public void setAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation)
set(int, long, PendingIntent)
, but this alarm will be allowed to execute
even when the system is in low-power idle 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.
type
- One of ELAPSED_REALTIME
, ELAPSED_REALTIME_WAKEUP
,
RTC
, or RTC_WAKEUP
.triggerAtMillis
- time in milliseconds that the alarm should go
off, using the appropriate clock (depending on the alarm type).operation
- Action to perform when the alarm goes off;
typically comes from IntentSender.getBroadcast()
.set(int, long, PendingIntent)
,
setExactAndAllowWhileIdle(int, long, android.app.PendingIntent)
,
cancel(android.app.PendingIntent)
,
Context.sendBroadcast(android.content.Intent)
,
Context.registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)
,
Intent.filterEquals(android.content.Intent)
,
ELAPSED_REALTIME
,
ELAPSED_REALTIME_WAKEUP
,
RTC
,
RTC_WAKEUP
public void setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation)
setExact(int, long, 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, android.app.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.
type
- One of ELAPSED_REALTIME
, ELAPSED_REALTIME_WAKEUP
,
RTC
, or RTC_WAKEUP
.triggerAtMillis
- time in milliseconds that the alarm should go
off, using the appropriate clock (depending on the alarm type).operation
- Action to perform when the alarm goes off;
typically comes from IntentSender.getBroadcast()
.set(int, long, android.app.PendingIntent)
,
setRepeating(int, long, long, android.app.PendingIntent)
,
setWindow(int, long, long, android.app.PendingIntent)
,
cancel(android.app.PendingIntent)
,
Context.sendBroadcast(android.content.Intent)
,
Context.registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)
,
Intent.filterEquals(android.content.Intent)
,
ELAPSED_REALTIME
,
ELAPSED_REALTIME_WAKEUP
,
RTC
,
RTC_WAKEUP
public void cancel(PendingIntent operation)
Intent
.
Any alarm, of any type, whose Intent matches this one (as defined by
Intent.filterEquals(android.content.Intent)
), will be canceled.operation
- IntentSender which matches a previously added
IntentSender. This parameter must not be null
.set(int, long, android.app.PendingIntent)
public void cancel(AlarmManager.OnAlarmListener listener)
AlarmManager.OnAlarmListener
.listener
- OnAlarmListener instance that is the target of a currently-set alarm.public void setTime(long millis)
millis
- time in milliseconds since the Epochpublic void setTimeZone(String timeZone)
TimeZone.setDefault(java.util.TimeZone)
if you just want to change the
time zone within your app, and even then prefer to pass an explicit
TimeZone
to APIs that require it rather than changing the time zone for
all threads.
On android M and above, it is an error to pass in a non-Olson timezone to this
function. Note that this is a bad idea on all Android releases because POSIX and
the TimeZone
class have opposite interpretations of '+'
and '-'
in the same non-Olson ID.
timeZone
- one of the Olson ids from the list returned by
TimeZone.getAvailableIDs(int)
public long getNextWakeFromIdleTime()
public AlarmManager.AlarmClockInfo getNextAlarmClock()
setAlarmClock(android.app.AlarmManager.AlarmClockInfo, android.app.PendingIntent)
method.AlarmManager.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
.setAlarmClock(android.app.AlarmManager.AlarmClockInfo, android.app.PendingIntent)
,
AlarmManager.AlarmClockInfo
,
ACTION_NEXT_ALARM_CLOCK_CHANGED
public AlarmManager.AlarmClockInfo getNextAlarmClock(int userId)
setAlarmClock(android.app.AlarmManager.AlarmClockInfo, android.app.PendingIntent)
method within the given user.AlarmManager.AlarmClockInfo
object describing the next upcoming alarm
clock event that will occur within the given user. If there are no alarm clock
events currently scheduled in that user, this method will return null
.setAlarmClock(android.app.AlarmManager.AlarmClockInfo, android.app.PendingIntent)
,
AlarmManager.AlarmClockInfo
,
ACTION_NEXT_ALARM_CLOCK_CHANGED