Alarm Manager Notification Android
adminApril 24 2021
Alarm Manager Notification Android
- Related Questions & Answers
- Selected Reading
AndroidMobile DevelopmentApps/Applications
Optimize your app with Doze and app stand by article from android developers blog help you out. If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle or setExactAndAllowWhileIdle. You can find source here. BaseColumns; CalendarContract.AttendeesColumns; CalendarContract.CalendarAlertsColumns; CalendarContract.CalendarCacheColumns; CalendarContract.CalendarColumns.
This example demonstrates how to 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.kt
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 the 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.
Android 12 Developer Preview is here! Try it out, and give us your feedback!
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.
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.
onCheckChanged listeners for toggle buttons.
- How to schedule repeating alarms with
AlarmManager
. - How to check if an alarm is already set up.
- How to cancel a repeating alarm.
- 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.
AlarmManager
.- 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.
activity_main.xml
layout file. Replace the 'Hello World' TextView
with the following ToggleButton
:Attribute | Value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- Extract your string resources.
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.
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.
createNotificationChannel()
.createNotificationChannel()
at the end of onCreate()
.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.
ic_stand_up
. For example, you could use the directions 'walk' icon: deliverNotification()
method, use the NotificationCompat.Builder
to build a notification using the notification icon and content intent. Set notification priority and other options.- Extract your string resources.
deliverNotification()
method, use the NotificationManager
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
.
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.
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.
- Initialize the
mNotificationManager
variable at the beginning of theonReceive()
method. You have to callgetSystemService()
from the passed-in context:
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.
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.
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
:
- 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.
Notifications In Android
- 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.
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.
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.
Answer these questions
AlarmManager? (All set()
methods use inexact timing, unless explicitly stated.)- API level 16
- API level 18
- API level 19
- API level 17
Alarm Manager Notification Android Security
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).
Alarm Manager Notification Android