'Android 12 - Foreground service launch restrictions

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.



Solution 1:[1]

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

Solution 2:[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.

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yavor Mitev
Solution 2 General Grievance
Solution 3 niranj1997