Schedule alarms

Alarms (based on the AlarmManager class) give you a way to perform time-based operations outside the lifetime of your application. For example, you could use an alarm to initiate a long-running operation, such as starting a service once a day to download a weather forecast.

Alarms have these characteristics:

  • They let you fire Intents at set times and/or intervals.

  • You can use them in conjunction with broadcast receivers to schedule jobs or WorkRequests to perform other operations.

  • They operate outside of your application, so you can use them to trigger events or actions even when your app is not running, and even if the device itself is asleep.

  • They help you minimize your app's resource requirements. You can schedule operations without relying on timers or continuously running services.

Set an inexact alarm

When an app sets an inexact alarm, the system delivers the alarm at some point in the future. Inexact alarms provide some guarantees regarding the timing of alarm delivery while respecting battery-saving restrictions such as Doze.

Developers can leverage the following API guarantees to customize the timing of inexact alarm delivery.

Deliver an alarm after a specific time

If your app calls set(), setInexactRepeating(), or setAndAllowWhileIdle(), the alarm never goes off before the supplied trigger time.

On Android 12 (API level 31) and higher, the system invokes the alarm within one hour of the supplied trigger time, unless any battery-saving restrictions are in effect such as battery saver or Doze.

Deliver an alarm during a time window

If your app calls setWindow(), the alarm never goes off before the supplied trigger time. Unless any battery-saving restrictions are in effect, the alarm is delivered within the specified time window, starting from the given trigger time.

If your app targets Android 12 or higher, the system can delay the invocation of a time-windowed inexact alarm by at least 10 minutes. For this reason, windowLengthMillis parameter values under 600000 are typically clipped to 600000. The system might accommodate smaller windows, especially if the alarm is scheduled to be triggered in the near future.

Deliver a repeating alarm at roughly regular intervals

If your app calls setInexactRepeating(), the system invokes multiple alarms:

  1. The first alarm goes off within the specified time window, starting from the given trigger time.
  2. Subsequent alarms usually go off after the specified time window elapses. The time between two consecutive invocations of the alarm can vary.

Set an exact alarm

The system invokes an exact alarm at a precise moment in the future.

Most apps can schedule tasks and events using inexact alarms to complete several common use cases. If your app's core functionality depends on a precisely-timed alarm—such as for an alarm clock app or a calendar app—then it's OK to use an exact alarm instead.