How Does Alarm Manager Work in Android? Complete Breakdown in Kotlin with a Real Example
Have you ever wondered how alarm clocks work on your phone? Or how a note-taking app reminds you of a meeting at exactly 2:30 PM? In Android, this is handled by a powerful system service — Alarm Manager. Although its name translates to "alarm manager," it is actually a universal task scheduler that allows you to run code at a specific time, even if your app is minimized or the phone is in sleep mode.
The topic of Alarm Manager Example is one of the most popular queries on StackOverflow (thousands of views and dozens of questions). And this is no surprise: beginners often get confused with PendingIntent, BroadcastReceiver, and numerous methods like set(), setRepeating(), or setExact(). In this article, we will break down a real working example in Kotlin that you can immediately apply to your project. No fluff — just code and explanations.
What is Alarm Manager and Why Do You Need It?
Alarm Manager is an Android system service that allows your app to perform actions at a specified time. The main feature is that it works independently of your app's lifecycle. You can set an alarm, close the app, turn off the screen — and Alarm Manager will still execute the task at the right moment.
Typical use cases:
- Event reminders (meetings, birthdays)
- Periodic data synchronization with the server
- Delayed notifications (e.g., "Don't forget to drink water in 2 hours")
- Scheduled background tasks
It is important to understand: Alarm Manager is not a precise timer for millisecond intervals. For that, there is Handler or Coroutines. Alarm Manager is optimized for system-level operation with power saving in mind.
Key Components: PendingIntent and BroadcastReceiver
Before writing code, let's understand two key concepts without which Alarm Manager does not work.
PendingIntent is a "deferred intent." You do not launch an action immediately, but package it into a special container that Alarm Manager will execute in the future. Imagine giving a courier a sealed envelope with instructions: "Open at exactly 3:00 PM." The envelope is the PendingIntent.
BroadcastReceiver is a receiver of broadcast messages. When Alarm Manager triggers, it sends a broadcast message that your BroadcastReceiver catches. This is where you write the code that should execute when the alarm goes off.
Step-by-Step Example: Creating an Alarm in Kotlin
Let's write a simple app that shows a notification 10 seconds after a button is pressed. This example is based on real code from a popular StackOverflow answer.
Step 1. Create a BroadcastReceiver
Create a new class that will handle the alarm trigger:
import android.content.BroadcastReceiverimport android.content.Contextimport android.content.Intentimport android.widget.Toast
class AlarmReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { // This code will execute when the alarm triggers Toast.makeText(context, "Alarm triggered!", Toast.LENGTH_LONG).show() }}For now, we just show a Toast. In a real app, this could be creating a notification, starting a service, or any other code.
Step 2. Register the BroadcastReceiver in the Manifest
For the system to know about our receiver, it must be registered in AndroidManifest.xml:
<receiver android:name=".AlarmRecei