Alarms and timers that ring on time (Android clock apps)
androidalarmmanagerexact-alarmsforeground-servicefull-screen-intentnotificationsclock
An alarm clock is the one kind of app Android still lets wake the phone at an exact minute, but only if it asks the right way. This article covers how to schedule, how to ring, and what to re-arm. It comes from Justin's Clock app (io.jhampton.clock, Kotlin + Compose, shipped 2026-10-27) and the midnight alarm in his Calendar app.
Scheduling
- Alarms the user set:
AlarmManager.setAlarmClock(AlarmClockInfo(time, showIntent), firePi). It is exact, it fires through Doze, and the system shows the alarm icon and "next alarm" time in the status bar.showIntentis what opens when the user taps that status-bar entry. - Timers and internal deadlines:
setExactAndAllowWhileIdle(RTC_WAKEUP, …). Exact and Doze-proof, but without the status-bar icon (a 5-minute pasta timer should not look like a wake-up alarm). - Permission:
USE_EXACT_ALARM(API 33+) is granted at install and cannot be revoked, but Google Play only allows it for alarm-clock and calendar apps; sideloaded apps can just use it. AddSCHEDULE_EXACT_ALARMwithandroid:maxSdkVersion="32"for Android 12 and 12L, where it is user-revocable. Always checkcanScheduleExactAlarms()(API 31+) and fall back tosetAndAllowWhileIdle, which may be minutes late but still fires. - One PendingIntent per alarm, keyed by request code and a unique data URI (
clock://alarm/<id>), so re-setting replaces instead of stacking. Makesync()idempotent: recompute every next ring time from the stored list and set or cancel each one. Then every change, and every re-arm trigger, just callssync(). - Cancel with
PendingIntent.getBroadcast(..., FLAG_NO_CREATE): it returns null if nothing is scheduled, so cancelling never creates a stray intent.
What clears alarms (re-arm on all of these)
AlarmManager forgets everything on reboot, and a wall-clock alarm computed from local time is wrong after a time-zone change. A manifest receiver should call sync() on BOOT_COMPLETED, MY_PACKAGE_REPLACED (an update cancels the app's alarms), TIME_SET, TIMEZONE_CHANGED and SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED. Call it on app start too.
A timer that ran out while the phone was off has a deadline in the past: on sync(), ring it immediately instead of scheduling it.
Ringing
- The alarm broadcast starts a foreground service (type
mediaPlayback,FOREGROUND_SERVICE_MEDIA_PLAYBACK). CallstartForegroundfirst thing inonStartCommand: Android kills a service started withstartForegroundServicethat does not promote itself within a few seconds. Hold a partial wake lock while ringing. - Play with
AudioAttributes.USAGE_ALARM(alarm volume stream, plays in silent mode) andCONTENT_TYPE_SONIFICATION; vibrate withVibrationAttributes.USAGE_ALARMon API 33+, or the vibration is muted with the ringer. A fade-in is justsetVolumestepped up from about 0.05 over 30 seconds. - The service's notification carries a full-screen intent to a ring activity with
showWhenLocked+turnScreenOn,singleInstance, an emptytaskAffinityandexcludeFromRecents, so it appears over the lock screen without dragging the main app's task along. On an unlocked phone the same notification shows as a heads-up with Snooze and Dismiss buttons. On Android 14+ checkNotificationManager.canUseFullScreenIntent(); sideloaded apps may not have it by default (see Writing a replacement phone (dialer) app for Android). - Stop by itself after a "silence after" period and leave a "Missed alarm" notification, so a phone left at home does not ring all day.
Timers and stopwatches
- Store the end time (epoch ms) of a running timer, not the remaining count, and the start time of a stopwatch. The UI derives the display from the clock, so the app can be killed and the numbers are still right on return.
- A running-timer notification needs no updates from the app:
setWhen(endAt)+setUsesChronometer(true)+setChronometerCountDown(true)lets the system count down. UsesetOnlyAlertOnce(true)so rebuilding it (pause, +1:00) does not buzz.
Being the system's clock app
An ACTION_SET_ALARM, SET_TIMER, SHOW_ALARMS and SHOW_TIMERS intent filter (category DEFAULT) on the main activity makes voice assistants and other apps hand alarms and timers to it. adb shell am start -a android.intent.action.SET_TIMER --ei android.intent.extra.alarm.LENGTH 5 --ez android.intent.extra.alarm.SKIP_UI true is a quick end-to-end test on the emulator.
A daily midnight job
For something that must happen at a calendar boundary (the Calendar app's date icon, see A launcher icon that changes (today's date on a calendar icon)), arm an exact RTC_WAKEUP alarm for next local midnight plus a couple of seconds, do the work in the receiver, and re-arm from there, plus the same reboot, update, time and zone triggers as above. Tested by setting the emulator clock to 23:59:50 as root.
Gotchas
- Untested on a real phone at the time of writing: lock-screen ringing on OEM skins, reboot rescheduling, and the Android 14+ full-screen-intent grant for a sideloaded APK. OEM battery managers are the usual reason alarms are late (see Doze, WorkManager backoff, and background sync that stalls).
See also
- Android notifications and PendingIntent pitfalls
- Doze, WorkManager backoff, and background sync that stalls
- Writing a replacement phone (dialer) app for Android
- Building a calendar app on Android's calendar provider