Reputation: 1891
I have an Android app on Play store for 8 years. Recently Google release Android S or 12 introduce some limit with Foreground service launch restrictions
and Exact alarm permission
https://developer.android.com/about/versions/12/behavior-changes-12#exact-alarm-permission
In the app I use foreground service and alarm clock to schedule update weather data from the cloud and device sensor and send notification to user, update the widget. But they said: Exact alarms should only be used for user-facing features so if I continue use those API, it is safe (with Google Play policy)?
I ask this because other solution like sticky notification with foreground service and workmanager not work as my requirements.
Upvotes: 48
Views: 78655
Reputation: 6731
Edit Aug 2024:
Only "Calendar" or "Wake Up" apps now are allowed to use the USE_EXACT_ALARM
permission.
As stated in the official documentation (https://developer.android.com/reference/android/Manifest.permission#USE_EXACT_ALARM):
If your app is already using SCHEDULE_EXACT_ALARM on older SDKs but needs USE_EXACT_ALARM on SDK 33 and above, then SCHEDULE_EXACT_ALARM should be declared with a max-sdk attribute
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
The doc also warns:
This is only intended for use by apps that rely on exact alarms for their core functionality
[...]
Keep in mind that this is a powerful permission and app stores may enforce policies to audit and review the use of this permission. Such audits may involve removal from the app store if the app is found to be misusing this permission.
The exact alarm should be used only if you are using any set*exact*
method to trigger an alarm at the exact specified date-time. Otherwise the "schedule" methods are enough, letting the system choose the proper nearest date-time to trigger the alarm accordingly to some system factors.
Upvotes: 12
Reputation: 4257
if you are testing android 12 then don't forget to add this line to Manifest
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
Update:
Android 13 generated a security exception and solution was to add below two lines in manifest:
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
Update for Android 14 :
crash for users on Android 14 because the permission is no longer granted by default. This means a runtime check should be done and if not granted then you should grant it programatically
Upvotes: 65
Reputation: 3100
Yes, the android.permission.SCHEDULE_EXACT_ALARM it's safe to use, on Android 12 this permission is automatically granted by the Android system but on Android 13 you need to check if the user has granted this permission.
So you need to add the permission to the manifest
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
And then you need to check if the permission was granted, if not granted then you need to redirect the user to the Alarms & Reminders page
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val alarmManager = ContextCompat.getSystemService(context, AlarmManager::class.java)
if (alarmManager?.canScheduleExactAlarms() == false) {
Intent().also { intent ->
intent.action = Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM
context.startActivity(intent)
}
}
}
Google also suggests that you need to check any changes on this permission by registering a Broadcast Receiver and check the changes on ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED
Upvotes: 47
Reputation: 639
Google states: "(when your app) requires precisely-timed actions". Your use case is "to schedule update weather data (…) send notification to user". While this might be user-facing, it doesn't seem to require to be precisely on a certain time. I would guess your app doesn't qualify.
The methods requiring the additional permission are currently: setExact()
, setExactAndAllowWhileIdle()
and setAlarmClock()
. Repeating alarms will always be inexact. Seems like getting processing weather data and device sensors is something repetitive anyway.
Upvotes: 2