I keep triggering the following error when my worker sets foreground info:
2024-05-15 16:27:21.616 508-5488 ActivityManager system_server W startForegroundService() not allowed due to mAllowStartForeground false: service com.myapp.dev/androidx.work.impl.foreground.SystemForegroundService
I’m hoping to find someone who successfully got the delayed job marked so it can run in the foreground. It reads like it should be possible according to:
https://developer.android.com/develop/background-work/services/foreground-services#bg-access-restrictions
But its vague if it will ever work in the case of a delayed worker.
Information around long running work:
https://developer.android.com/develop/background-work/background-tasks/persistent/how-to/long-running
I setting in the manifest (I used special just to see if could get anything working):
<uses-permission android:name="android.permission.FOREGROUND_SERVICE">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE">
...
<service
android:name="androidx.work.impl.foreground.SystemForegroundService"
android:foregroundServiceType="specialUse"
tools:node="merge"
/>
I’ve googled for other people successfully doing it, but I havent found anyone succeeding, I instead see most people dealing with the exception and continuing silently (but that risks my worker getting killed before it done).
The only time my worker successfully makes does setForeground is if my activity is in the foreground when the worker runs.
Here are some other stackoverflows having similar issues:
Fatal Android 12: Exception: startForegroundService() not allowed due to mAllowStartForeground false
Here is code that I think is defining the various reasons if I could get set my worker would be able to be in the foreground:
https://github.com/Reginer/aosp-android-jar/blob/main/android-31/src/android/os/PowerExemptionManager.java#L117