- Related Questions & Answers
- Alarmmanager Android Example
- Alarm Manager Android Example
- Alarmmanager Android Example Service
- Alarmmanager Android Example Github
- Android Alarm
- Selected Reading
Import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.app.AlarmManager import android.content.Context import android.app.PendingIntent import android.content.Intent import kotlinx.android.synthetic.main.activitymain. import java.util. class MainActivity: AppCompatActivity private val REQUESTCODE = 100. Android platform documentation. Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
This example demonstrates how do I use AlarmManager in android.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
Step 3 − Add the following code to src/MainActivity.java
Step 4 - Add the following code to androidManifest.xml
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –
Click here to download the project code.
This practical codelab is part of Unit 3: Working in the background in the Android Developer Fundamentals (Version 2) course. You will get the most value out of this course if you work through the codelabs in sequence:
- For the complete list of codelabs in the course, see Codelabs for Android Developer Fundamentals (V2).
- For details about the course, including links to all the concept chapters, apps, and slides, see Android Developer Fundamentals (Version 2).
Note: This course uses the terms 'codelab' and 'practical' interchangeably.
Introduction
In previous lessons, you learned how to make your app respond when a user taps a button or a notification. You also learned how to make your app respond to system events using broadcast receivers. But what if your app needs to take action at a specific time, for example for a calendar notification? In this case, you would use AlarmManager
. The AlarmManager
class lets you launch and repeat a PendingIntent
at a specified time, or after a specified interval.
In this practical, you create a timer that reminds the user to stand up every 15 minutes.
What you should already know
You should be able to:
- Implement
onCheckChanged
listeners for toggle buttons. - Set up and use custom broadcast receivers.
- Send notifications.
What you'll learn
- How to schedule repeating alarms with
AlarmManager
. - How to check if an alarm is already set up.
- How to cancel a repeating alarm.
What you'll do
- Set a repeating alarm to notify you every 15 minutes.
- Use a
ToggleButton
to set and keep track of the alarm. - Use
Toast
messages to notify the user when the alarm is turned on or off.
Stand Up! is an app that helps you stay healthy by reminding you to stand up and walk around every 15 minutes. It uses a notification to let you know when 15 minutes have passed. The app includes a toggle button that can turn the alarm on and off.
1.1 Create the Stand Up! project layout
- In Android Studio, create a new project called 'Stand Up!'. Accept the default options and use the Empty Activity template.
- Open the
activity_main.xml
layout file. Replace the 'Hello World'TextView
with the followingToggleButton
:
Attribute | Value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- Extract your string resources.
1.2 Set up the setOnCheckedChangeListener() method
The Stand Up! app includes a toggle button that is used to set and cancel the alarm, as well as to visually represent the alarm's status. To set the alarm when the toggle is turned on, your app uses the onCheckedChangeListener()
method.
In MainActivity.java
, inside the onCreate()
method, implement the following steps:
- Find the
ToggleButton
byid
.
- Call
setOnCheckedChangeListener()
on theToggleButton
instance, and begin entering 'new OnCheckedChangeListener
'. Android Studio autocompletes the method for you, including the requiredonCheckedChanged()
override method.
The first parameter in onCheckedChanged()
is the CompoundButton
that the user tapped, which in this case is the alarm ToggleButton
. The second parameter is a boolean
that represents the state of the ToggleButton
, that is, whether the toggle is on or off.
- In the
onCheckedChanged()
method, set up anif-else
block using theboolean
parameter. If the alarm was turned on or off, display aToast
message to the user.
- Extract your string resources.
The next step is to create the notification that reminds the user to stand up every 15 minutes. For now, the notification is delivered immediately when the toggle is set.
2.1 Create the notification
In this step, you create a deliverNotification()
method that posts the reminder to stand up and walk.
Implement the following steps in MainActivity.java
:
- Create a member variable called
mNotificationManager
of the typeNotificationManager
.
- In the
onCreate()
method, initializemNotificationManager
usinggetSystemService()
.
- Create member constants for the notification ID and the notification channel ID. You will use these to display the notification. To learn more about notifications, see the Notifications overview.
Create a notification channel
For Android 8.0 (API level 27) and higher, to display notifications to the user, you need a notification channel.
Create a notification channel:
- Create a method called
createNotificationChannel()
. - Call
createNotificationChannel()
at the end ofonCreate()
.
Set the notification content Intent
- Create a method called
deliverNotification()
that takes theContext
as an argument and returns nothing.
- In the
deliverNotification()
method, create anIntent
that you will use for the notification content intent.
- In the
deliverNotification()
method, after the definition ofcontentIntent,
create aPendingIntent
from the content intent. Use thegetActivity()
method, passing in the notification ID and using theFLAG_UPDATE_CURRENT
flag:
Note: PendingIntent
flags tell the system how to handle the situation when multiple instances of the same PendingIntent
are created (meaning that the instances contain the same Intent
). The FLAG_UPDATE_CURRENT
flag tells the system to use the old Intent
but replace the extras
data. Because you don't have any extras in this Intent
, you can use the same PendingIntent
over and over.
Add a notification icon and build the notification
- Use the Image Asset Studio to add an image asset to use as the notification icon. Choose any icon you find appropriate for this alarm and name it
ic_stand_up
. For example, you could use the directions 'walk' icon: - In the
deliverNotification()
method, use theNotificationCompat.Builder
to build a notification using the notification icon and content intent. Set notification priority and other options.
- Extract your string resources.
Deliver the notification and test your app
- At the end of the
deliverNotification()
method, use theNotificationManager
to deliver the notification:
- In
onCreate()
, calldeliverNotification()
when the alarm toggle button is turned on, passing in the activity context. - In
onCreate()
, callcancelAll()
on theNotificationManager
if the toggle is turned off to remove the notification.
- Run the app, and check that the notification is delivered.
At this point there is no alarm at all: the notification is immediately delivered when the alarm toggle button is turned on. In the next task you implement the AlarmManager
to schedule and deliver the notification every 15 minutes.
Now that your app can send a notification, it's time to implement the main component of your app: the AlarmManager
. This class will periodically deliver the reminder to stand up. AlarmManager
has many kinds of alarms built into it, both one-time and periodic, exact and inexact. To learn more about the different kinds of alarms, see Schedule repeating alarms.
AlarmManager
, like notifications, uses a PendingIntent
that it delivers with the specified options. Because of this, AlarmManager
can deliver the Intent
even when the app is no longer running.
A broadcast receiver receives the broadcast intent and delivers the notification.
Alarms do not fire when the device is in Doze mode (idle). Instead, alarms are deferred until the device exits Doze. To guarantee that alarms execute, you can use setAndAllowWhileIdle()
or setExactAndAllowWhileIdle()
. You can also use the new WorkManager
API, which is built to perform background work either once or periodically. For details, see Schedule tasks with WorkManager
.
The AlarmManager
can trigger one-time or recurring events that occur even when your app is not running. For real-time clock (RTC
) alarms, schedule events using System.currentTimeMillis()
. For elapsed-time (ELAPSED_REALTIME
) alarms, schedule events using elapsedRealtime()
. Deliver a PendingIntent
when events occur.
For more about the available clocks and how to control the timing of events, see SystemClock
.
3.1 Create the broadcast receiver
Create a broadcast receiver that receives the broadcast intents from the AlarmManager
and reacts appropriately:
- In Android Studio, select File > New > Other > Broadcast Receiver.
- Enter
AlarmReceiver
for the Class Name. Make sure that the Exported checkbox is cleared so that other apps can't invoke this broadcast receiver.
Android Studio creates a subclass of BroadcastReceiver
with the required method, onReceive()
. Android Studio also adds the receiver to your AndroidManifest
file.
Implement the following steps in broadcast receiver's AlarmReceiver.java
file:
- Remove the entire default implementation from the
onReceive()
method, including the line that raises theUnsupportedOperationException
. - Cut and paste the
deliverNotification()
method from theMainActivity
class to theAlarmReceiver
class and call it fromonReceive()
. You may notice some variables highlighted in red. You define them in the next step. - Copy the
NOTIFICATION_ID
,PRIMARY_CHANNEL_ID
, andmNotificationManager
member variables from theMainActivity
class into theAlarmReceiver
class.
Alarmmanager Android Example
- Initialize the
mNotificationManager
variable at the beginning of theonReceive()
method. You have to callgetSystemService()
from the passed-in context:
3.2 Set up the broadcast pending intent
The AlarmManager
is responsible for delivering the PendingIntent
at a specified interval. This PendingIntent
delivers an intent letting the app know it is time to update the remaining time in the notification.
Implement the following steps in MainActivity.java
, inside onCreate()
:
- Create an
Intent
callednotifyIntent
. Pass in the context andAlarmReceiver
class.
- Create the notify
PendingIntent
. Use the context, theNOTIFICATION_ID
variable, the new notify intent, and theFLAG_UPDATE_CURRENT
flag.
3.3 Set the repeating alarm
Now you use the AlarmManager
to deliver the broadcast every 15 minutes. For this task, the appropriate type of alarm is a`n inexact, repeating alarm that uses elapsed time and wakes the device up if it is asleep. The real-time clock is not relevant here, because you want to deliver the notification every 15 minutes.
Implement the following steps in MainActivity.java
:
- Initialize the
AlarmManager
inonCreate()
by callinggetSystemService()
.
- In Android Studio, select File > New > Other > Broadcast Receiver.
- Enter
AlarmReceiver
for the Class Name. Make sure that the Exported checkbox is cleared so that other apps can't invoke this broadcast receiver.
Android Studio creates a subclass of BroadcastReceiver
with the required method, onReceive()
. Android Studio also adds the receiver to your AndroidManifest
file.
Implement the following steps in broadcast receiver's AlarmReceiver.java
file:
- Remove the entire default implementation from the
onReceive()
method, including the line that raises theUnsupportedOperationException
. - Cut and paste the
deliverNotification()
method from theMainActivity
class to theAlarmReceiver
class and call it fromonReceive()
. You may notice some variables highlighted in red. You define them in the next step. - Copy the
NOTIFICATION_ID
,PRIMARY_CHANNEL_ID
, andmNotificationManager
member variables from theMainActivity
class into theAlarmReceiver
class.
Alarmmanager Android Example
- Initialize the
mNotificationManager
variable at the beginning of theonReceive()
method. You have to callgetSystemService()
from the passed-in context:
3.2 Set up the broadcast pending intent
The AlarmManager
is responsible for delivering the PendingIntent
at a specified interval. This PendingIntent
delivers an intent letting the app know it is time to update the remaining time in the notification.
Implement the following steps in MainActivity.java
, inside onCreate()
:
- Create an
Intent
callednotifyIntent
. Pass in the context andAlarmReceiver
class.
- Create the notify
PendingIntent
. Use the context, theNOTIFICATION_ID
variable, the new notify intent, and theFLAG_UPDATE_CURRENT
flag.
3.3 Set the repeating alarm
Now you use the AlarmManager
to deliver the broadcast every 15 minutes. For this task, the appropriate type of alarm is a`n inexact, repeating alarm that uses elapsed time and wakes the device up if it is asleep. The real-time clock is not relevant here, because you want to deliver the notification every 15 minutes.
Implement the following steps in MainActivity.java
:
- Initialize the
AlarmManager
inonCreate()
by callinggetSystemService()
.
- In the
onCheckedChanged()
method, remove the call todeliverNotification()
. - In the
onCheckedChanged()
method, callsetInexactRepeating()
on the alarm manager instance inside theif
case (when the alarm is toggled on).
You use the setInexactRepeating()
alarm because it is more resource-efficient to use inexact timing, which lets the system bundle alarms from different apps together. Also, it's acceptable for your app to deviate a little bit from the exact 15-minute interval.
The setInexactRepeating()
method takes four arguments:
- The alarm type. In this case only the relative time is important, and you want to wake the device if it's asleep, so use
ELAPSED_REALTIME_WAKEUP
. - The trigger time in milliseconds. Use the current elapsed time, plus 15 minutes. To get the current elapsed time, call
SystemClock``.elapsedRealtime()
. Then use a built-inAlarmManager
constant to add 15 minutes to the elapsed time. - The time interval in milliseconds. You can use the
AlarmManager.INTERVAL_FIFTEEN_MINUTES
constant. - The
PendingIntent
to be delivered.
Note: Because you are accessing the AlarmManager
and notifyPendingIntent
instances from an anonymous inner class, Android Studio may make these instances final
. If it doesn't, you have to make them final
yourself.
- Inside the
else
case (when the alarm is toggled off), cancel the alarm by callingcancel()
on theAlarmManager
. Pass in the pending intent used to create the alarm.
Keep the call to cancelAll()
on the NotificationManager
, because turning the alarm toggle off should still remove any existing notification.
The AlarmManager
now delivers your broadcast 15 minutes after the alarm is set, and every 15 minutes after that.
- Run your app. If you don't want to wait 15 minutes to see the notification, change the trigger time to
SystemClock.elapsedRealtime()
to see the notification immediately. You can also change the interval to a shorter time to make sure that the repeated alarm is working.
You now have an app that can schedule and perform a repeated operation, even if the app is no longer running. Go ahead, exit the app completely—the notification is still delivered.
You still need to fix one thing to ensure a proper user experience: if the app is closed, the toggle button resets to the off state, even if the alarm has already been set. To fix this, you need to check the state of the alarm every time the app is launched.
3.4 Check the state of the alarm
To track the state of the alarm, you need a boolean
variable that is true
if the alarm exists, and false
otherwise. To set this boolean
, you can call PendingIntent.getBroadcast()
with the FLAG_NO_CREATE
flag. If a PendingIntent
exists, that PendingIntent
is returned; otherwise the call returns null
.
Implement the following steps in MainActivity.java
:
Alarm Manager Android Example
- Create a
boolean
that istrue
ifPendingIntent
is notnull
, andfalse
otherwise. Use thisboolean
to set the state of theToggleButton
when your app starts. This code has to come before thePendingIntent
is created. (Otherwise it always returnstrue
.)
Note: The flag determines what happens if a PendingIntent
whose intent matches the intent you are trying to create already exists. The NO_CREATE
flag returns null
unless a PendingIntent
with a matching Intent
exists.
- Set the state of the toggle right after you define
alarmUp
:
This ensures that the toggle is always on if the alarm is set, and off otherwise. You now have a repeated scheduled alarm to remind the user to stand up every 15 minutes.
- Run your app. Switch on the alarm. Exit the app. Open the app again. The alarm button shows that the alarm is on.
Android Studio project: StandUp
Note: All coding challenges are optional and are not prerequisites for later lessons.
Alarmmanager Android Example Service
The AlarmManager
class also handles the usual kind of alarm clocks, the kind that wake you up in the morning. On devices running API 21 and higher, you can get information about the next alarm clock of this kind by calling getNextAlarmClock()
on the alarm manager.
Add a button to your app that displays a Toast
message. The toast shows the time of the next alarm clock that the user has set.
AlarmManager
allows you to schedule tasks based on the real-time clock or on the elapsed time since boot.AlarmManager
provides a variety of alarm types, both periodic and one-time.- Alarms do not fire when the device is in Doze mode (idle). Scheduled alarms are deferred until the device exits Doze.
- If you need tasks to be completed even when the device is idle, you can use
setAndAllowWhileIdle()
orsetExactAndAllowWhileIdle(``)
. You can also use theWorkManager
API, which is built to perform background work either once or periodically. For more information, see Schedule tasks with WorkManager. - Whenever possible, use the inexact-timing version of the
AlarmManager
. Inexact timing minimizes the load caused by multiple users' devices or multiple apps performing a task at the exact same time. AlarmManager
uses pending intents to perform its operations. You schedule broadcasts, services, and activities using the appropriatePendingIntent
.
The related concept documentation is in 8.2: Alarms. Free play casino bonus no deposit.
Android developer documentation:
Other resources:
This section lists possible homework assignments for students who are working through this codelab as part of a course led by an instructor. It's up to the instructor to do the following:
- Assign homework if required.
- Communicate to students how to submit homework assignments.
- Grade the homework assignments.
Instructors can use these suggestions as little or as much as they want, and should feel free to assign any other homework they feel is appropriate.
If you're working through this codelab on your own, feel free to use these homework assignments to test your knowledge.
Build and run an app
Make an app that delivers a notification when the time is 11:11 AM. The screen displays a toggle switch that turns the alarm on and off.
Note: The notification might look a little different, depending on the API level of the device.
Alarmmanager Android Example Github
Answer these questions
Android Alarm
Question 1
In which API level did inexact timing become the default for AlarmManager
? (All set()
methods use inexact timing, unless explicitly stated.) Weather guru accurate weather forecasts 2 3 3.
- API level 16
- API level 18
- API level 19
- API level 17
Submit your app for grading
Guidance for graders
Check that the app has the following features:
- The alarm uses exact timing. The code checks whether the device's API level is higher than 19, and uses the
setExact()
method if it is. - The app shows a notification when the time is 11:11 AM.
To find the next practical codelab in the Android Developer Fundamentals (V2) course, see Codelabs for Android Developer Fundamentals (V2).
For an overview of the course, including links to the concept chapters, apps, and slides, see Android Developer Fundamentals (Version 2).