Using AlarmManager like a pro
All of us must have used AlarmManager in some way in our projects but there a lot of times when our alarms get cancelled or there are bugs in the implementation. Today we will learn what all measures we should take to make sure that all our alarms are set correctly. We will also understand what all scenarios we should consider whenever we are using the AlarmManager.
Firstly let’s get the basics right. There are two types of alarms that you can set. You can either set a one time alarm or a repeating alarm. Let’s quickly have a look at both of these alarms.
For non repeating alarms we just have to specify at what time we need the alarm. The following alarm will fire after 1 min.
For repeating alarms, we have to first set at what time we need the alarm and then the duration after which the alarm should repeat itself. The following alarm will fire everyday. AlarmManager.INTERVAL_DAY is nothing but the time in milliseconds for a day, instead you can also specify any time of your choice in milliseconds.
So it is very easy to set these alarms but now let’s discuss the cases when these alarms get cancelled or behave in an unusual manner.
All alarms get cancelled when the phone reboots
By default all alarms get cancelled when a device shuts down. To tackle this we need to set the alarms again when the user restarts their device. Let’s have a look how this is done.
The above permission needs to be added in the manifest so that the app can receive the ACTION_BOOT_COMPLETED broadcast after the system finishes booting.
Don’t forget to add the receiver in the manifest with the action BOOT_COMPLETED. If you are wondering what android:enabled=”false” is it simply means that the receiver will not be called unless it is explicitly enabled in the app.
The above code enables the receiver. You should preferably add it in the first activity of your app.
The BroadcastReceiver above gets called whenever the user reboots their device. Here we will set the alarm again but the important thing is we can’t use the values passed through the intent to set the alarm. This is because these values will be null when the system reboots. We need to pass values from a permanent source of memory like a SQL database.
All alarms get cancelled when the system time is changed
Another case when the Alarms get cancelled is when the user changes their system time or date from the settings. Let’s see what we can do to tackle this situation.
Firstly we need to register a receiver in the manifest with the action TIME_SET.
The following BroadcastReceiver will get called every time the user changes the system time. You just have to set the alarms again in the onReceive.
All past Alarms are fired immediately
When we are setting our alarms it is very important to check that the time that we are setting the alarm is not of the past because these alarms will get fired as soon as they are set. A simple check such as the following will ensure that no past alarms are fired.
These were some of the points that you should keep in mind if you are using the AlarmManager to set alarms for may be showing periodic notifications once a day.
Here is the link of the sample project that I made for this story. Feel free to check it out.
Thanks for reading! If you enjoyed this story, please click the 👏 button and share to help others! Feel free to leave a comment 💬 below. Have feedback? Let’s connect on Twitter.