Skip to content
tezvyn:
Menu

AlarmManager: Scheduling Precise Future Tasks in Android

Source: developer.android.comMediumHow cards are made

AlarmManager is Android's system-wide alarm clock for your app, letting you run code at a specific time, even if your app is closed. Use it for time-sensitive tasks like calendar notifications. The footgun: alarms are wiped on reboot and must be re-registered.

Why it exists

Applications often need to perform operations long after they are closed, triggered by the passage of time. Android needed a way for apps to schedule these future tasks with the OS itself, ensuring they run even if the app's process is killed or the device is asleep.

The mental model

Think of AlarmManager as setting an alarm on a real, physical clock that belongs to the Android system, not your app. You tell the system, "At 8:00 AM, please run this specific piece of my code." The system then takes full responsibility for waking the device (if requested) and triggering your code at or near that time.

How it works

You get an instance of AlarmManager from the system context. You then create a PendingIntent, which is a token that gives the system permission to execute a piece of your app's code on its behalf. This intent might start a service or send a broadcast. You then call a method like setExactAndAllowWhileIdle(), passing the trigger time, the type of alarm (e.g., RTC_WAKEUP to wake the device from sleep), and your PendingIntent. When the alarm fires, the system executes the PendingIntent.

When to use it

Use AlarmManager for tasks that are user-visible and must run at a specific, precise time. The canonical use cases are alarm clock apps and calendar event notifications. These are tasks where being a few minutes late ruins the user experience.

When not to use it

Do not use AlarmManager for deferrable background work that doesn't need to run at an exact time, like syncing data or pre-fetching content. For that, WorkManager is the modern, recommended solution. WorkManager respects battery optimizations and system health, can chain tasks, and handles constraints like network availability. Using AlarmManager for frequent, non-critical tasks is a major cause of battery drain and is restricted by modern Android versions.

One canonical example

To set a precise alarm that wakes the device, an app first requests the SCHEDULE_EXACT_ALARM permission. It then gets the AlarmManager service, creates a PendingIntent pointing to a BroadcastReceiver, and calls alarmManager.setExactAndAllowWhileIdle(triggerAtMillis, pendingIntent). The receiver handles the event, perhaps by showing a notification. Critically, after a device reboot, a separate BOOT_COMPLETED receiver must run to re-schedule this same alarm, as all alarms are cleared on restart.

Interview question

Which scenario is the most appropriate use case for Android's AlarmManager?

  • a.Displaying a critical reminder notification to the user at a specific, pre-set time, even if the device is idle.Correct
  • b.Synchronizing an app's database with a remote server daily, respecting network and power constraints.
  • c.Scheduling a series of chained background operations that can be retried if they fail.
  • d.Downloading large media files in the background when the device is connected to Wi-Fi and not actively in use.
Why?

AlarmManager is designed for precise, time-sensitive tasks that are user-visible, such as critical reminders or alarm clocks. The other options describe deferrable background work that is better suited for WorkManager, which optimizes for system health and battery life.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles