Package org.robolectric.shadows
Class ShadowAlarmManager
java.lang.Object
org.robolectric.shadows.ShadowAlarmManager
Shadow for
AlarmManager
.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Represents a set alarm. -
Field Summary
Modifier and TypeFieldDescriptionstatic final long
static final long
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
cancel
(AlarmManager.OnAlarmListener listener) protected void
cancel
(PendingIntent operation) protected void
protected boolean
void
Immediately removes the given alarm from the list of scheduled alarms (and then reschedules it in the case of a repeating alarm) and fires it.protected AlarmManager.AlarmClockInfo
Deprecated.Returns a list of all scheduled alarms, ordered from earliest time to latest time.Returns the earliest scheduled alarm.static void
reset()
protected void
set
(int type, long triggerAtMs, long windowLengthMs, long intervalMs, AlarmManager.OnAlarmListener listener, Handler targetHandler, WorkSource workSource) protected void
set
(int type, long triggerAtMs, long windowLengthMs, long intervalMs, PendingIntent operation, WorkSource workSource) protected void
set
(int type, long triggerAtMs, long windowLengthMs, long intervalMs, String tag, AlarmManager.OnAlarmListener listener, Handler targetHandler, WorkSource workSource) protected void
set
(int type, long triggerAtMs, PendingIntent operation) protected void
set
(int type, long triggerAtMs, String tag, AlarmManager.OnAlarmListener listener, Handler handler) protected void
setAlarmClock
(AlarmManager.AlarmClockInfo info, PendingIntent operation) protected void
setAndAllowWhileIdle
(int type, long triggerAtMs, PendingIntent operation) static void
setAutoSchedule
(boolean autoSchedule) When set to true, automatically schedules alarms to fire at the appropriate time (with respect to the main Looper time) when they are set.static void
setCanScheduleExactAlarms
(boolean scheduleExactAlarms) Sets the schedule exact alarm state reported byAlarmManager.canScheduleExactAlarms()
, but has no effect otherwise.protected void
setExact
(int type, long triggerAtMs, PendingIntent operation) protected void
setExact
(int type, long triggerAtTime, String tag, AlarmManager.OnAlarmListener listener, Handler targetHandler) protected void
setExact
(int type, long triggerAtMs, String tag, Executor executor, WorkSource workSource, AlarmManager.OnAlarmListener listener) protected void
setExactAndAllowWhileIdle
(int type, long triggerAtMs, PendingIntent operation) protected void
setExactAndAllowWhileIdle
(int type, long triggerAtMs, String tag, Executor executor, WorkSource workSource, AlarmManager.OnAlarmListener listener) protected void
setInexactRepeating
(int type, long triggerAtMs, long intervalMillis, PendingIntent operation) protected void
setPrioritized
(int type, long windowStartMs, long windowLengthMs, String tag, Executor executor, AlarmManager.OnAlarmListener listener) protected void
setRepeating
(int type, long triggerAtMs, long intervalMs, PendingIntent operation) protected void
setTimeZone
(String timeZone) protected void
setWindow
(int type, long windowStartMs, long windowLengthMs, PendingIntent operation) protected void
setWindow
(int type, long windowStartMs, long windowLengthMs, String tag, AlarmManager.OnAlarmListener listener, Handler handler) protected void
setWindow
(int type, long windowStartMs, long windowLengthMs, String tag, Executor executor, AlarmManager.OnAlarmListener listener) protected void
setWindow
(int type, long windowStartMs, long windowLengthMs, String tag, Executor executor, WorkSource workSource, AlarmManager.OnAlarmListener listener)
-
Field Details
-
WINDOW_EXACT
public static final long WINDOW_EXACT- See Also:
-
WINDOW_HEURISTIC
public static final long WINDOW_HEURISTIC- See Also:
-
-
Constructor Details
-
ShadowAlarmManager
public ShadowAlarmManager()
-
-
Method Details
-
reset
-
setAutoSchedule
public static void setAutoSchedule(boolean autoSchedule) When set to true, automatically schedules alarms to fire at the appropriate time (with respect to the main Looper time) when they are set. This means that a test as below could be expected to pass:shadowOf(alarmManager).setAutoSchedule(true); AlarmManager.OnAlarmListener listener = mock(AlarmManager.OnAlarmListener.class); alarmManager.setExact( ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 10, "tag", listener, new Handler(Looper.getMainLooper())); shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(10)); verify(listener).onAlarm();
Alarms are always scheduled with respect to the trigger/window start time - there is no emulation of alarms being reordered, rescheduled, or delayed, as might happen on a real device. If emulating this is necessary, see
fireAlarm(ScheduledAlarm)
.AlarmManager.OnAlarmListener
alarms will be run on the correct Handler/Executor as specified when the alarm is set. -
set
-
set
@Implementation(minSdk=24) protected void set(int type, long triggerAtMs, @Nullable String tag, AlarmManager.OnAlarmListener listener, @Nullable Handler handler) -
setRepeating
@Implementation protected void setRepeating(int type, long triggerAtMs, long intervalMs, PendingIntent operation) -
setWindow
@Implementation(minSdk=19) protected void setWindow(int type, long windowStartMs, long windowLengthMs, PendingIntent operation) -
setWindow
@Implementation(minSdk=24) protected void setWindow(int type, long windowStartMs, long windowLengthMs, @Nullable String tag, AlarmManager.OnAlarmListener listener, @Nullable Handler handler) -
setWindow
@Implementation(minSdk=34) protected void setWindow(int type, long windowStartMs, long windowLengthMs, @Nullable String tag, Executor executor, AlarmManager.OnAlarmListener listener) -
setWindow
@Implementation(minSdk=34) protected void setWindow(int type, long windowStartMs, long windowLengthMs, @Nullable String tag, Executor executor, WorkSource workSource, AlarmManager.OnAlarmListener listener) -
setPrioritized
@Implementation(minSdk=31) protected void setPrioritized(int type, long windowStartMs, long windowLengthMs, @Nullable String tag, Executor executor, AlarmManager.OnAlarmListener listener) -
setExact
@Implementation(minSdk=19) protected void setExact(int type, long triggerAtMs, PendingIntent operation) -
setExact
@Implementation(minSdk=24) protected void setExact(int type, long triggerAtTime, @Nullable String tag, AlarmManager.OnAlarmListener listener, @Nullable Handler targetHandler) -
setAlarmClock
@RequiresApi(21) @Implementation(minSdk=21) protected void setAlarmClock(AlarmManager.AlarmClockInfo info, PendingIntent operation) -
set
@Implementation(minSdk=19) protected void set(int type, long triggerAtMs, long windowLengthMs, long intervalMs, PendingIntent operation, @Nullable WorkSource workSource) -
set
@Implementation(minSdk=24) protected void set(int type, long triggerAtMs, long windowLengthMs, long intervalMs, @Nullable String tag, AlarmManager.OnAlarmListener listener, @Nullable Handler targetHandler, @Nullable WorkSource workSource) -
set
@Implementation(minSdk=24) protected void set(int type, long triggerAtMs, long windowLengthMs, long intervalMs, AlarmManager.OnAlarmListener listener, @Nullable Handler targetHandler, @Nullable WorkSource workSource) -
setExact
@Implementation(minSdk=31) protected void setExact(int type, long triggerAtMs, @Nullable String tag, Executor executor, WorkSource workSource, AlarmManager.OnAlarmListener listener) -
setInexactRepeating
@Implementation protected void setInexactRepeating(int type, long triggerAtMs, long intervalMillis, PendingIntent operation) -
setAndAllowWhileIdle
@Implementation(minSdk=23) protected void setAndAllowWhileIdle(int type, long triggerAtMs, PendingIntent operation) -
setExactAndAllowWhileIdle
@Implementation(minSdk=23) protected void setExactAndAllowWhileIdle(int type, long triggerAtMs, PendingIntent operation) -
setExactAndAllowWhileIdle
@Implementation(minSdk=34) protected void setExactAndAllowWhileIdle(int type, long triggerAtMs, @Nullable String tag, Executor executor, @Nullable WorkSource workSource, AlarmManager.OnAlarmListener listener) -
cancel
-
cancel
-
cancelAll
-
setTimeZone
-
canScheduleExactAlarms
-
getNextAlarmClock
@RequiresApi(21) @Implementation(minSdk=21) @Nullable protected AlarmManager.AlarmClockInfo getNextAlarmClock() -
getNextScheduledAlarm
Deprecated.Prefer to usesetAutoSchedule(boolean)
in combination with incrementing time to actually run alarms and test their side-effects.Returns the earliest scheduled alarm and removes it from the list of scheduled alarms. -
peekNextScheduledAlarm
Returns the earliest scheduled alarm. -
getScheduledAlarms
Returns a list of all scheduled alarms, ordered from earliest time to latest time. -
fireAlarm
Immediately removes the given alarm from the list of scheduled alarms (and then reschedules it in the case of a repeating alarm) and fires it. The given alarm must on the list of scheduled alarms prior to being fired.Generally prefer to use
setAutoSchedule(boolean)
in combination with advancing time on the main Looper in order to test alarms - however this method can be useful to emulate rescheduled, reordered, or delayed alarms, as may happen on a real device. -
setCanScheduleExactAlarms
public static void setCanScheduleExactAlarms(boolean scheduleExactAlarms) Sets the schedule exact alarm state reported byAlarmManager.canScheduleExactAlarms()
, but has no effect otherwise.
-
setAutoSchedule(boolean)
in combination with incrementing time to actually run alarms and test their side-effects.