Class ShadowAlarmManager

java.lang.Object
org.robolectric.shadows.ShadowAlarmManager

@Implements(android.app.AlarmManager.class) public class ShadowAlarmManager extends Object
Shadow for AlarmManager.
  • Field Details

  • Constructor Details

    • ShadowAlarmManager

      public ShadowAlarmManager()
  • Method Details

    • reset

      @Resetter public static void 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

      @Implementation protected void set(int type, long triggerAtMs, PendingIntent operation)
    • 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 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 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

      @Implementation protected void setAlarmClock(AlarmManager.AlarmClockInfo info, PendingIntent operation)
    • set

      @Implementation 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

      @Implementation protected void cancel(PendingIntent operation)
    • cancel

      @Implementation(minSdk=24) protected void cancel(AlarmManager.OnAlarmListener listener)
    • cancelAll

      @Implementation(minSdk=34) protected void cancelAll()
    • setTimeZone

      @Implementation protected void setTimeZone(String timeZone)
    • canScheduleExactAlarms

      @Implementation(minSdk=31) protected boolean canScheduleExactAlarms()
    • getNextAlarmClock

      @Implementation @Nullable protected AlarmManager.AlarmClockInfo getNextAlarmClock()
    • getNextScheduledAlarm

      @Deprecated @Nullable public ShadowAlarmManager.ScheduledAlarm getNextScheduledAlarm()
      Deprecated.
      Prefer to use setAutoSchedule(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

      @Nullable public ShadowAlarmManager.ScheduledAlarm peekNextScheduledAlarm()
      Returns the earliest scheduled alarm.
    • getScheduledAlarms

      public List<ShadowAlarmManager.ScheduledAlarm> getScheduledAlarms()
      Returns a list of all scheduled alarms, ordered from earliest time to latest time.
    • fireAlarm

      public void fireAlarm(ShadowAlarmManager.ScheduledAlarm alarm)
      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 by AlarmManager.canScheduleExactAlarms(), but has no effect otherwise.