23

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

https://developer.android.com/about/versions/12/behavior-changes-12#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.

Pratik Fagadiya
  • 1,195
  • 7
  • 21
DzungPV
  • 1,561
  • 3
  • 18
  • 24
  • I update for anyone search this: Google update a policy about Exact Alarm API here https://support.google.com/googleplay/android-developer/answer/13161072. If you still want to use, using android.permission.SCHEDULE_EXACT_ALARM and request the user on Android 14+. – DzungPV Jul 01 '23 at 03:30

5 Answers5

29

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" />
15

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

Adelino
  • 2,530
  • 4
  • 20
  • 32
  • You're slightly wrong, on Android 12 `SCHEDULE_EXACT_ALARM` permission automatically granted by the system, but can be revoked by the system or user anytime. Check the docs https://developer.android.com/training/scheduling/alarms#using-schedule-exact-permission – Timur Panzhiev Feb 01 '23 at 16:16
  • @TimurPanzhiev please check what I said in the bottom part, by checking the flag ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED you can handle the revoked action and react to it. By default after a fresh install on Android 12 this permission is granted, them you need to handle the changes of this permission like always, but by default its granted. – Adelino Feb 02 '23 at 14:06
2

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.

Michael
  • 629
  • 6
  • 15
0

effective solution you need to add the permission to the manifest before <application

-1

From what you've mentioned, you're talking about user-facing features.

A hypothetical example of the opposite would be Facebook forcing synchronization of user data at some specific time. That would be bad because it's preferable not to force a schedule on those types of things as it doesn't matter whether it happens at a specific time or a minute later when system resources are not used by some other service.

Also, "should" means it's a recommendation. Facebook can do the above, but it would be a less optimal solution. It's best to leave control over those kinds of services to Android as it would likely do a better job at distributing resources and preventing lag. So in other words, you not listening to their recommendation won't get your app removed from the app store or something like that.

Also, the paragraph you quoted from the second link, has a link to examples of acceptable use cases, and it mentions alarm apps. This is likely why your question was downvoted.

Tin Svagelj
  • 124
  • 1
  • 13