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:
NOTE: Refer Setup Android Notification. NOTE: To support multiple alarm, use a different requestCode for each alarm. Refer Android AlarmManager: Multiple Alarm With Arguments/Parameters. NOTE: AlarmManager is removed when the app is uninstalled, and it seems the Alarm is cancelled after APK update (not sure if this is true or always true). The Alarm Manager holds a CPU wake lock which guarantees that the phone will not sleep until you have finished handling the broadcast. It can also be used to perform some repetitive task outside the lifetime of your application, which means even if your app is not running, you can initiate long-running operation. Such as downloading weather. Creating an Alarm to send a Notification. In order to create an alarm to trigger a notification, you will need to do the following: Create notification channels for each pet type. Determine the time and date to schedule the alarm. Create a PendingIntent to add to the alarm. Schedule it with the AlarmManager. Alarm manager not working properly for scheduled notifications. Discussion in ' Android Development ' started by Shivang111, Aug 29, 2017. Shivang111 Lurker.
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:
What you'll learn
What you'll do
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
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:
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.
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 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:
Set the notification content
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |