Change theme
Help
Press space for more information.
Show links for this issue (Shortcut: i, l)
Copy issue ID
Previous Issue (Shortcut: k)
Next Issue (Shortcut: j)
Sign in to use full features.
Vote: I am impacted
Notification menu
Refresh (Shortcut: Shift+r)
Go home (Shortcut: u)
Pending code changes (auto-populated)
View issue level access limits(Press Alt + Right arrow for more information)
Unintended behavior
View staffing
Description
Version used: 2.7.1
Devices/Android versions reproduced on: Android 12
Brief description of the problem.
The application receives a ForegroundServiceStartNotAllowedException if the application is brought into the background in a second or the same second when the worker starts.
Detailed description of the problem.
My application uses MVVM architecture along with LiveData. The worker is launched in Fragment using LiveData from ViewModel. This ensures that the worker only runs in the foreground. But my application can receive links from other applications.
Activity with:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
After the application receives a link from another application, the worker is launched and setForeground method is called in doWork method. And if I move the application to the background when the SplashScreen is shown, then ForegroundServiceStartNotAllowedException is thrown.
I tried to check if the application is in the background before calling the setForeground method. And if the application is in the background, then a function is called that waits for the application to move to the foreground. This has reduced the number of crashes. But I still get it. You just need to move the application to the background in a second or at the same time when the worker starts.
Before the setForeground method is called, the application successfully passes all checks that it is in the foreground, but after that it takes some time before the worker calls ContextCompat.startForegroundService(mAppContext, intent); But during this time, mAllowStartForeground becomes false and an exception is thrown.
The problem is exacerbated by the fact that there is no way to wrap the setForeground in a try-catch, and handle the error. For example, wait for the application to move to the foreground and at that time send a notification to the user asking them to open the application in order to continue working.
One more moment. I use a chain of workers that are launched sequentially passing the result of their work to the next worker. If I move the application to the background when the first worker successfully starts the foreground service, then all subsequent workers also successfully start the foreground service. But if an error occurred in the first one, then all subsequent workers also receive an error when calling the setForeground method.
I use this method to check.
fun isAppInBackground(): Boolean {
val processInfo = ActivityManager.RunningAppProcessInfo()
ActivityManager.getMyMemoryState(processInfo)
return processInfo.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
}