Search code examples
androidandroid-serviceandroid-workmanagerandroid-12foregroundnotification

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

  • 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