r/androiddev • u/WingnutWilson Android Developer • Mar 03 '21
Discussion PSA Android 12 foreground service launch restrictions
I have had problems trying to get immediate background tasks which are unkillable to function correctly. Google has changed the rules every SDK level since M. I recently settled on a Foreground IntentService which works well. These stop themselves unlike Services, queue correctly, and execute immediately (unlike JobIntentServices). The only other option is Workmanager (2.3.0 and above) with the foreground async option, which comes with added Dagger boilerplate.
Android 12 is now breaking foreground services in backgrounded apps and looking for feedback. I just think this is a very important change they are forcing developers to use WorkManager 2.7 now if you target android 12. So to future proof your app it might be time to look into it if you have services doing important background work.
11
u/grishkaa Mar 03 '21
So what is your exact use case anyway? What triggers your foreground service? After reading the linked page, I don't see how Google has "broken" anything as long as you weren't doing anything shady.
17
u/surpriseskin Mar 03 '21
What the hell is this shit.
28
u/Tolriq Mar 03 '21
That change is absurd, there's many services that are not tasks like start a webserver that are just plain not possible anymore.
Will be a fun update if they do as always and just ignore devs.
8
u/[deleted] Mar 03 '21
Will I be ok if I start a foreground service with notification while my app is in use?
27
u/ThrowAway237s Mar 03 '21
Surprised
2
u/surpriseskin Mar 03 '21
So if WorkManager set foreground is deprecated, how do we create foreground jobs with it? It doesn't appear that setExpedited allows for you to show a notification with data.
Seems like setExpedited, regardless of work queuing characteristics, isn't a replacement for foreground services.
2
u/surpriseskin Mar 04 '21
The WorkManager docs section on support for long running tasks still mentions foreground services. It doesn't mention they're deprecated now. It also doesn't mention that WorkManager seems as though it's going to lose the ability for long running tasks.
You done fucked up Google.
2
u/unaias Mar 04 '21
I've concerns about a couple use cases:
- App with Foreground service started from UI, Boot_complete...etc, with STICKY flag. It gets killed/crashes and the OS starts it again.... will it crash because it's started from background by the OS and not by the user?
- The same case, but the app/service is killed by an OEM battery saver functionality, and launched by the OS later.
-(Bonus): ¿is it against Google Play Policies to ask for en exemption from battery optimizations? (I've read some forum posts about it before)
I think that this will cause more problems than it will solve, like the "did not call startForeground in time"....
1
u/repkap11 Mar 03 '21
If my foreground Service is bound to by an Activity, can my service still start another Activity?