Android 12 brought several changes and one of them, that raises some questions, is the new exact alarm permission. This article is a quick guide about this new permission, showing why, how and when to implement the changes. It is the first time that I write an article in this format, so feedback is welcome. 😊
What is this permission?
The new exact alarm permission (SCHEDULE_EXACT_ALARM
) was created to save system resources. Alarms that must be executed in an exact time may be triggered when the phone is on power-saving mode or Doze, making the app consumes more battery than it should.
When should I use an exact alarm?
Google recommends the usage of this alarm in two situations:
- The application is a clock or a timer app
- The app has precision events, such as calendars or reminder apps
What is the type of this permission?
The type of this permission is “Special app access”. You can find it on Settings > Apps > Special app access > Alarms & reminders
. This permission is already granted at install-time. In order to revoke it, the user needs to manually go to this screen.
What happens when the user revokes this permission?
First of all, all the exact alarms already set will be deleted and will no longer work. Besides that, if any function setting an exact alarm is called, the app will crash with SecurityException
.
My app uses AlarmManager, should I change anything?
It depends. If the application is using any AlarmManager
method that define an exact alarm, some changes are required. The methods are:
setAlarmClock()
setExact()
setExactAndAllowWhileIdle()
If the application is using AlarmManager
with methods such as set()
, setInexactRepeating()
, setAndAllowWhileIdle()
or setWindow()
, no changes are required.
My app uses WorkManager, should I change anything?
No. WorkManager
was created respecting all the system optimizations and there is no guarantee that it will be executed in the exact time that was set up. This new requirement applies only for exact alarms using AlarmManager
.
What do I need to update in my app?
There are some steps required in order to keep using exact alarms on Android 12, as well as some precautions to maintain the stability of your application.
1. Add the new permission on AndroidManifest.xml
The first step is adding the new SCHEDULE_EXACT_ALARM
permission on AndroidManifest.xml
. If Android Studio did not find it, make sure that the app is compiled on API 31 (compileSdkVersion = 31
).
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
package="com.escodro.alkaa"> | |
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" /> | |
<application/> | |
</manifest> |
2. Check if the permission is granted before setting alarms
Since this permission may be revoked anytime by the user, it is recommended that a verification is executed before setting any new exact alarm. For that, AlarmManager
has a new method:
val alarmManager: AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager | |
val hasPermission: Boolean = alarmManager.canScheduleExactAlarms() |
The canScheduleExactAlarms()
returns a boolean informing if the permission is granted or not.
3. Redirect user to grant the permission
In case the verification method returns false
, the recommendation is showing information to the user why this permission is important for your app and redirect them to “Special app access” screen. To do so, we can use the following Intent
:
val intent = Intent().apply { | |
action = Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM | |
} | |
startActivity(intent) |
It should be noted that this Intent
will not redirect the user to your app permission. The user will be redirected to the “Special app access” screen listing all the apps that may need this permission. Therefore it is a good idea to be clear to the user before redirecting them.
4. Register a BroadcastReceiver to receive the information about the permission
Another recommendation is creating a BroadcastReceiver
to receive an Intent
when the user grants the permission again for your application. This information is very important because, as mentioned earlier, when the user revokes the permission, all the exact alarms will be deleted. Based on this info, it is possible to set them up again.
For apps that already works with alarms, the behavior is similar when the phone is turned off: all the alarms are removed by the system and when it is turned back on, the app receives RECEIVE_BOOT_COMPLETED
to reschedule them.
The BroadcastReceiver
will be similar to following:
internal class MyBroadcastReceiver : BroadcastReceiver() { | |
override fun onReceive(context: Context, intent: Intent) { | |
when (intent.action) { | |
AlarmManager.ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED -> { | |
// reschedule all the exact alarms | |
} | |
} | |
} |
The ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED
will be sent to both runtime and manifest receivers. In our example, registering it via manifest will look like:
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.escodro.alarm"> | |
<application> | |
<receiver | |
android:name=".TaskReceiver" | |
android:exported="false"> | |
<intent-filter> | |
<action android:name="android.app.action.SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED" /> | |
</intent-filter> | |
</receiver> | |
</application> | |
</manifest> |
Job Offers
It is very important to point out that this broadcast will only be sent by the system when the user grants the permission again. This broadcast will not be sent when the user revokes this permission. Depending on the user experience that your app provides, it is an interesting idea to verify and inform the user about this permission every time they opens your app.
What’s next?
The changes to support exact alarms on Android 12 are relatively simple, but good testing is essential once these changes may impact the usability and stability of your app.
If you want to see this change in a more structured application, I recommend this pull request from Alkaa. My personal app is a to-do reminder app, which was directly impacted by this new requirement. This PR shows how the change impacts a more “real world” application overall.
External resources
There are some official docs I recommend reading with more details about this new permission and other Android 12 change requirements.
- Behavior changes: Apps targeting Android 12 — Exact alarm permission
- Schedule alarms — Set an exact alarm
- Manifest permissions — SCHEDULE_EXACT_ALARM
- AlarmManager — ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED
Thanks a lot for reading my article! ❤️
Thanks to Bruno Kenji Tiba.