Package org.robolectric.shadows
Class ShadowAlarmManager
java.lang.Object
org.robolectric.shadows.ShadowAlarmManager
Shadow for
AlarmManager.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents a set alarm. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longstatic final long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcancel(AlarmManager.OnAlarmListener listener) protected voidcancel(PendingIntent operation) protected voidprotected booleanvoidImmediately 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.AlarmClockInfoDeprecated.Returns a list of all scheduled alarms, ordered from earliest time to latest time.Returns the earliest scheduled alarm.static voidreset()protected voidset(int type, long triggerAtMs, long windowLengthMs, long intervalMs, AlarmManager.OnAlarmListener listener, Handler targetHandler, WorkSource workSource) protected voidset(int type, long triggerAtMs, long windowLengthMs, long intervalMs, PendingIntent operation, WorkSource workSource) protected voidset(int type, long triggerAtMs, long windowLengthMs, long intervalMs, String tag, AlarmManager.OnAlarmListener listener, Handler targetHandler, WorkSource workSource) protected voidset(int type, long triggerAtMs, PendingIntent operation) protected voidset(int type, long triggerAtMs, String tag, AlarmManager.OnAlarmListener listener, Handler handler) protected voidsetAlarmClock(AlarmManager.AlarmClockInfo info, PendingIntent operation) protected voidsetAndAllowWhileIdle(int type, long triggerAtMs, PendingIntent operation) static voidsetAutoSchedule(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 voidsetCanScheduleExactAlarms(boolean scheduleExactAlarms) Sets the schedule exact alarm state reported byAlarmManager.canScheduleExactAlarms(), but has no effect otherwise.protected voidsetExact(int type, long triggerAtMs, PendingIntent operation) protected voidsetExact(int type, long triggerAtTime, String tag, AlarmManager.OnAlarmListener listener, Handler targetHandler) protected voidsetExact(int type, long triggerAtMs, String tag, Executor executor, WorkSource workSource, AlarmManager.OnAlarmListener listener) protected voidsetExactAndAllowWhileIdle(int type, long triggerAtMs, PendingIntent operation) protected voidsetExactAndAllowWhileIdle(int type, long triggerAtMs, String tag, Executor executor, WorkSource workSource, AlarmManager.OnAlarmListener listener) protected voidsetInexactRepeating(int type, long triggerAtMs, long intervalMillis, PendingIntent operation) protected voidsetPrioritized(int type, long windowStartMs, long windowLengthMs, String tag, Executor executor, AlarmManager.OnAlarmListener listener) protected voidsetRepeating(int type, long triggerAtMs, long intervalMs, PendingIntent operation) protected voidsetTimeZone(String timeZone) protected voidsetWindow(int type, long windowStartMs, long windowLengthMs, PendingIntent operation) protected voidsetWindow(int type, long windowStartMs, long windowLengthMs, String tag, AlarmManager.OnAlarmListener listener, Handler handler) protected voidsetWindow(int type, long windowStartMs, long windowLengthMs, String tag, Executor executor, AlarmManager.OnAlarmListener listener) protected voidsetWindow(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.OnAlarmListeneralarms 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, String tag, AlarmManager.OnAlarmListener listener, Handler handler) -
setRepeating
@Implementation protected void setRepeating(int type, long triggerAtMs, long intervalMs, PendingIntent operation) -
setWindow
@Implementation protected void setWindow(int type, long windowStartMs, long windowLengthMs, PendingIntent operation) -
setWindow
@Implementation(minSdk=24) protected void setWindow(int type, long windowStartMs, long windowLengthMs, String tag, AlarmManager.OnAlarmListener listener, Handler handler) -
setWindow
@Implementation(minSdk=34) protected void setWindow(int type, long windowStartMs, long windowLengthMs, String tag, Executor executor, AlarmManager.OnAlarmListener listener) -
setWindow
@Implementation(minSdk=34) protected void setWindow(int type, long windowStartMs, long windowLengthMs, String tag, Executor executor, WorkSource workSource, AlarmManager.OnAlarmListener listener) -
setPrioritized
@Implementation(minSdk=31) protected void setPrioritized(int type, long windowStartMs, long windowLengthMs, String tag, Executor executor, AlarmManager.OnAlarmListener listener) -
setExact
-
setExact
@Implementation(minSdk=24) protected void setExact(int type, long triggerAtTime, String tag, AlarmManager.OnAlarmListener listener, Handler targetHandler) -
setAlarmClock
@Implementation protected void setAlarmClock(AlarmManager.AlarmClockInfo info, PendingIntent operation) -
set
@Implementation protected void set(int type, long triggerAtMs, long windowLengthMs, long intervalMs, PendingIntent operation, WorkSource workSource) -
set
@Implementation(minSdk=24) protected void set(int type, long triggerAtMs, long windowLengthMs, long intervalMs, String tag, AlarmManager.OnAlarmListener listener, Handler targetHandler, WorkSource workSource) -
set
@Implementation(minSdk=24) protected void set(int type, long triggerAtMs, long windowLengthMs, long intervalMs, AlarmManager.OnAlarmListener listener, Handler targetHandler, WorkSource workSource) -
setExact
@Implementation(minSdk=31) protected void setExact(int type, long triggerAtMs, 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, String tag, Executor executor, WorkSource workSource, AlarmManager.OnAlarmListener listener) -
cancel
-
cancel
-
cancelAll
-
setTimeZone
-
canScheduleExactAlarms
-
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.