ACTION_SET_ALARM from service on Android 10+

292 Views Asked by At

I have an application that has such function as Alarm Clock. For some people, it is more convenient to use an external Alarm Clock for some reason. So, for such people, I have an option "Use external Alarm Clock". I used to start ACTION_SET_ALARM from my service before Android 10 like this:

...
 
        val intent = Intent(AlarmClock.ACTION_SET_ALARM).apply {
            putExtra(AlarmClock.EXTRA_MESSAGE, message)
            putExtra(AlarmClock.EXTRA_HOUR, calendar.hourOfDay)
            putExtra(AlarmClock.EXTRA_MINUTES, calendar.minute)
            putExtra(AlarmClock.EXTRA_SKIP_UI, true)
            putExtra(AlarmClock.EXTRA_VIBRATE, appPreferences.alarmVibration)
            ringtone?.let {
                putExtra(AlarmClock.EXTRA_RINGTONE, it)
            }
            flags = Intent.FLAG_ACTIVITY_NEW_TASK
        }
        if (intent.resolveActivity(appContext.packageManager) != null) {
            appContext.startActivity(intent)
...

Since Android 10, there are some restrictions: https://developer.android.com/guide/components/activities/background-starts

So, I cannot start activity for enabling external alarms anymore, and it's very annoying.

Does anybody know how to solve this problem?

For now, I'm thinking about asking users to grant SYSTEM_ALERT_WINDOW permission but it seems like a wrong way. Anyway, it doesn't solve all the problems because of restrictions on Android Go.

1

There are 1 best solutions below

2
On

for android 9 and above you can use android JobService other then background service as they are more optimized for device and the user, or you can use Foreground service as regular service.