skip to content

A dash-cam app schedules a planned trip's recording with AlarmManager.setExact, but it fires late while the phone sits parked in Doze; why, and what do you change?

level: seniorimportance: should knowfreq 40%

answer

  1. the phone is parked and idle
  2. Doze holds standard alarms
  3. maintenance windows
  4. the AllowWhileIdle variant
  5. canScheduleExactAlarms() can turn false

basics

~10 s

Doze defers standard AlarmManager alarms, setExact() included, to its next maintenance window. Schedule with setExactAndAllowWhileIdle(), hold SCHEDULE_EXACT_ALARM, check canScheduleExactAlarms() first, and fall back to setAndAllowWhileIdle() when the user has not granted exact alarms.

solid answer

~40 s

A parked phone that is unplugged, still and dark enters Doze, which defers standard alarms, `setExact()` and `setWindow()` included, to the next maintenance window, so the alarm fires late. `setExactAndAllowWhileIdle()` fires during Doze, rate-limited per app, and `setAlarmClock()` makes the system leave Doze for a user-visible alarm. Exact calls need a permission on Android 12 (API 31) and later, or they throw `SecurityException`. A dash cam uses `SCHEDULE_EXACT_ALARM`, which the user grants and can revoke and which Android 14 no longer pre-grants to fresh installs targeting API 33+, because `USE_EXACT_ALARM` is limited to apps whose core, user-facing feature depends on precise alarms. Call `canScheduleExactAlarms()` first, send the user to `ACTION_REQUEST_SCHEDULE_EXACT_ALARM` when it is `false`, fall back to `setAndAllowWhileIdle()`, and reschedule on `ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED`.

code

kotlin · 18 lines
kotlin
fun scheduleTripRecording(context: Context, triggerAtMillis: Long) {
    val alarmManager = context.getSystemService(AlarmManager::class.java)
    val operation = PendingIntent.getBroadcast(
        context,
        TRIP_ALARM_REQUEST_CODE,
        Intent(context, TripAlarmReceiver::class.java),
        PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
    )
    val canExact = Build.VERSION.SDK_INT < Build.VERSION_CODES.S ||
        alarmManager.canScheduleExactAlarms()
    if (canExact) {
        // Fires on time even while the parked phone dozes.
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAtMillis, operation)
    } else {
        // No exact-alarm access: still fires in Doze, just not to the minute.
        alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAtMillis, operation)
    }
}

go deeper

for a junior

Know that an idle, unplugged phone enters Doze and that ordinary alarms wait for its maintenance windows.

for a middle

Explain the AlarmManager ladder from set() to setAlarmClock(), and the difference between SCHEDULE_EXACT_ALARM and USE_EXACT_ALARM.

for a senior

Diagnose a late alarm from Doze and standby buckets, handle a revoked permission with canScheduleExactAlarms(), and reschedule on the grant broadcast.

for a principal

Decide which features truly need exact alarms, given their battery cost and permission friction, and set inexact or job-based scheduling as the default.

## Why setExact() fires late in Doze **Doze** is the power-saving mode a phone enters when it sits unplugged, stationary and with the screen off, exactly the parked dash cam's situation. While dozing, the system suspends network access, ignores wake locks, stops `JobScheduler` jobs and **defers standard `AlarmManager` alarms, `setExact()` and `setWindow()` included, to the next maintenance window**. Maintenance windows come periodically and less often the longer the device stays idle, so a recording scheduled for 07:00 with `setExact()` can run long after 07:00. The alarm was exact; Doze held it. **App Standby buckets** add a second limiter: the system sorts apps into active, working set, frequent, rare and restricted buckets, and the bucket decides how often an app's jobs run and its alarms trigger while on battery. `UsageStatsManager.getAppStandbyBucket()` tells an app where it stands. ## The AlarmManager ladder | Call | Exact? | Fires in Doze? | Exact-alarm permission | |---|---|---|---| | `set()` / `setWindow()` | no (short windows usually stretched to 10 minutes on Android 12+) | deferred | no | | `setAndAllowWhileIdle()` | no | yes | no | | `setExact()` | yes, outside battery restrictions | deferred | yes | | `setExactAndAllowWhileIdle()` | yes | yes, rate-limited | yes | | `setAlarmClock()` | yes, never adjusted | system leaves Doze for it | yes | - `setExactAndAllowWhileIdle()` runs in Doze and puts the app on a temporary power-exemption list for about 10 seconds so it can finish its work. The system spaces these alarms out per app while idle (the Doze guide states once per nine minutes), and may deliver them out of order. - `setAlarmClock()` is for alarms the user sees as an alarm clock; the system exits Doze shortly before it fires. - The `setExact` overloads that take an `OnAlarmListener` need no exact-alarm permission, but the system may cancel listener alarms once the process has no running components, so they do not fit a recording scheduled hours ahead. ## The exact-alarm permissions Since **Android 12 (API 31)**, apps targeting API 31 or higher must hold an "Alarms & reminders" permission to call the exact methods, or they get a `SecurityException` (apps on the power-save exemption list are excused): - **`SCHEDULE_EXACT_ALARM`** is granted by the user on the Alarms & reminders special-access screen, and the user or the system can revoke it. The alarms guide states it is not pre-granted to fresh installs of apps targeting Android 13 (API 33) or higher, a default that came with Android 14; an app that already held it keeps it when the device upgrades. - **`USE_EXACT_ALARM`**, available to apps targeting Android 13 (API 33), is granted automatically and cannot be revoked, but the alarms guide lists it for limited use cases, subject to Google Play policy: apps whose core, user-facing function depends on precisely timed alarms, the guide's examples being alarm clocks and calendars. A dash cam is not that, so it uses `SCHEDULE_EXACT_ALARM`. - When the user revokes `SCHEDULE_EXACT_ALARM`, the app is stopped and its exact alarms are cancelled. When the user grants it, the system sends `ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED`; no broadcast is sent on revocation. - For apps targeting Android 14, holding `SCHEDULE_EXACT_ALARM` no longer keeps the app in the working-set bucket or better. ## A scheduling routine that survives all of this 1. Call `canScheduleExactAlarms()` before scheduling (it exists from API 31; older versions can always schedule exact alarms). 2. If it returns `true`, schedule with `setExactAndAllowWhileIdle()` and an `RTC_WAKEUP` time so the alarm runs even when the parked phone dozes. 3. If it returns `false`, explain in the UI why on-time starts matter and open `Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM`; meanwhile fall back to `setAndAllowWhileIdle()`, which still fires in Doze, just not to the minute. 4. Register a receiver for `ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED`, re-check `canScheduleExactAlarms()` (the user may have revoked it again) and reschedule, the same way you reschedule after `ACTION_BOOT_COMPLETED`. ## What the alarm may start An exact alarm that completes a user-requested action is an exemption from the Android 12 background-start rule, and `setExactAndAllowWhileIdle()` alarms may start a foreground service from the background. For apps targeting Android 14, though, a `camera` foreground service created in the background still fails its while-in-use check, and alarms are not on that exemption list. So the alarm's receiver for the planned trip posts a notification that the driver taps to begin recording, or starts work that needs no while-in-use permission. The notification itself is covered in the notifications lesson.

  • The alarm now fires on time. Can its receiver start the camera foreground service directly?
    The exact alarm exempts the start from the Android 12 background-start rule, but for apps targeting Android 14 a `camera` service created in the background fails its while-in-use check with `SecurityException`, and alarms are not on that exemption list. The receiver should post a notification the driver taps to begin recording.
  • When is USE_EXACT_ALARM the right permission instead?
    When precisely timed alarms are the app's core, user-facing function, as in an alarm clock or calendar. It is granted automatically and cannot be revoked, which is why its use is limited under Google Play policy. Apps outside that set declare `SCHEDULE_EXACT_ALARM` and handle the user saying no.
  • What happens to the app's alarms if the user revokes Alarms & reminders?
    The app is stopped and its exact alarms, those set with `setExact()`, `setExactAndAllowWhileIdle()` and `setAlarmClock()`, are cancelled. No broadcast announces the revocation; the app finds out from `canScheduleExactAlarms()` on its next run and should reschedule with an inexact call.

Doze is an office building after hours where the mail room delivers only on its scheduled rounds: a letter marked for 7:00 sharp still waits for the next round. An allow-while-idle alarm is a courier with an after-hours pass, let in on time but only every so often.

  • setExact() always fires at the exact time, even while the device dozes.
  • USE_EXACT_ALARM is the safe default for any app that wants punctual alarms.
  • Once granted, SCHEDULE_EXACT_ALARM stays granted, so one check at install is enough.
  • ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED tells the app its access was revoked.
  • setExactAndAllowWhileIdle() can fire every few seconds while the device dozes.

Assumes targetSdk 33+ on Android 14 or later: exact alarms need a permission from Android 12 (API 31), USE_EXACT_ALARM exists from Android 13 (API 33), and fresh installs are not pre-granted SCHEDULE_EXACT_ALARM from Android 14; checked against API 37.