Class ShadowAlarmManager

    • Constructor Detail

      • ShadowAlarmManager

        public ShadowAlarmManager()
    • Method Detail

      • 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)
      • 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)
      • 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)
      • 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)
      • 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)
      • cancel

        @Implementation
        protected void cancel​(PendingIntent operation)
      • cancelAll

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

        @Implementation
        protected void setTimeZone​(String timeZone)
      • canScheduleExactAlarms

        @Implementation(minSdk=31)
        protected boolean canScheduleExactAlarms()
      • 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.
      • 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.