Android 12 - Foreground service launch restrictions
Asked Answered
M

3

24

I'm developing an SDK that needs to startForeground service from the background. Because it uses background location and Bluetooth-related works. If the application is killed, the monitoring is performing in the background. That's why I'm using the foreground service. There is a condition that starts the foreground service from the background.

Currently, my SDK using Service to handle this job. But Android 12 on-words it doesn't support to start service from the background.

I'm trying to start the service from the background the below exception throws.

ForegroundServiceStartNotAllowedException: Service.startForeground() not allowed due to mAllowStartForeground false

How can I use WorkManager to fix this issue, all my handling is done by the Service class and how can I pass the Service object to Worker class and start this job inside the Worker class.

Actually, my project is based on beacon technology. and the beacon signals are used to show different recommendations to the user.

In my current implementation, if the application is killed by the user, and also accepts the foreground service, the SDK will be run in the background. and detect the beacon and provide appropriate actions.

My implementation is that, if the application initializes my SDK with the foreground service "OFF" Then sometime later, when the application is in the background and trying to start the foreground service from the background this exception throws. The foreground service-related decisions are held by the server-side API. I'm periodically checking whether the server-side value is changed or not, and if the value is changed the changed action is reflected in the SDK.

Moureaux answered 17/9, 2021 at 12:4 Comment(6)
developer.android.com/about/versions/12/…Stave
medium.com/androiddevelopers/…Herewith
"I'm developing a SDK which needs to startForeground service from background" -- why? Why does it need to be a foreground service, and why does it need to be started from the background?Emotion
Because it use background location and Bluetooth related works. If the application is killed, the monitoring is happening in the background. That's why I'm using foreground service. There is a condition which start the foreground service from background.Moureaux
I am starting my foreground service and displaying the notification from the dashboard of my app when the user clicks a button only. And I am also getting the same issue on Android 12. I only track location and user activity. I hope this helps with some more info. I only have a few crashes so far but still concerns me.Denisse
You may wish to explore abandoning the Android Service construct entirely for use cases like these. Check out one alternative approach herePiny
I
8

There is no one in the world that can give you an answer. The idea of all these restrictions is that we as developers need to optimize our applications. So if this is not possible for you it means most likely that you need to optimize the way you do your work. For this to happen you need to provide more info of what exactly events you are receiving, what is exactly your use case, etc.

https://developer.android.com/about/versions/12/foreground-services#cases-fgs-background-starts-allowed

As you can see there is info about exceptions for:

Your app receives a Bluetooth broadcast that requires the BLUETOOTH_CONNECT or BLUETOOTH_SCAN permissions.

But there is nothing in your question saying that your use case might relate to this.

Also, I don't understand how the app might be killed, but you keep working in the background.

Also if you want to constantly do something - why there is an event when you are in the background. Just when the user opens the app - start the service and keep it going.

You can also just "hack" it and ask the user to remove you from battery optimization.

https://developer.android.com/training/monitoring-device-state/doze-standby#support_for_other_use_cases

Immuno answered 18/9, 2021 at 16:28 Comment(1)
Yavor Mitev thank you for the response. I just updated my question added some more information about my project please have look at this.Moureaux
O
6

Earlier we were using Service to run background tasks. But, due to Android 12 - Foreground service launch restrictions, we will not be able to invoke Service for performing background tasks for Android 12+.

So from now on, from targetSdk 31, Service can be invoked only when the application is in the foreground. When the application is closed or when the application went to the background, invoking Service using startForegroundService will cause ForegroundServiceStartNotAllowedException.

So to perform background tasks, we need to use Worker instead of Service. Please refer to this answer to get an idea of how it is implemented. Hope it helps. Also, refer to the below links to get a high-level overview of what changes needs to be done.

  1. Android 12 Behavior Changes

  2. Work Requests

Otis answered 5/5, 2022 at 17:51 Comment(6)
Thanks for the update. unfortunately we can't use worker class instead of service. is there any possible solution for using service. please mentionMoureaux
Without Worker, I don't think there is any other option. But as a work around, you could mention the Target SDK as 30, instead of 31 to use Service. Anyhow in future, you need to surely implement Worker instead of Service to focus on future releases. Please let us know, if you find any other option to implement. It would be helpful for many :)Otis
@Otis while this is may be a suitable workaround for many apps, there are times when a Worker is not suitable (long-running indefinite tasks such as tracking a run for example). Regardless, Google documents exemptions and yet is ignoring them. It is clearly a bug in Android 12 and the only way to start a service from the background is to target 30.Vergara
@Vergara . So you may be right!. Because of a bug in Android 12, they would have mentioned it as a Service restriction. So, as you said, as of now, better work around would be is to use Target SDK 30 instead of 31 to make use of Service. And wait for Android team to fix this issue before they make target SDK 31 as a mandate for publishing apps on Google Play. Hope they'll fix it soon and make developer's life easier! :)Otis
I am already using Worker in foreground mode, same issue, but my app target SDK is still os 11, planning soon to upgrade, not sure it will solve problem.Convert
@Otis i need to get user current location without service it not possible.Outspoken
G
2

According to the official docs, if your app does one of the following, it should be able to start an FGS:

Your app receives a Bluetooth broadcast that requires the BLUETOOTH_CONNECT or BLUETOOTH_SCAN permissions.

or

Your app receives an event that's related to geofencing or activity recognition transition.

Those two seem like pretty good candidates for your use-case, at least how I understood it.

Garman answered 5/11, 2021 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.