After upgrading my phone to Android 12 I started to some unexpected behavior with the expedited work job. That is the code I had:
val workRequest = OneTimeWorkRequestBuilder<UploadWorker>()
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
.build()
WorkManager.getInstance(context).enqueueUniqueWork(UNIQUE_WORK_NAME, ExistingWorkPolicy.REPLACE, workRequest)
And also I had the getForegroundInfo()
overrided to return ForegroundInfo
instance.
On Android < 12 my work job worked fine: it is long-running and when it got started, the notification was shown in notification trey, and the job was done.
Now, with Android 12 I don't see any notification icon and if I'll turn the screen off, the job is cancelled exactly in one minute. And immediately gets relaunched.
If I explicitly call setForeground()
in doWork()
method (I don't know what is the difference), it slightly change the behavior: the notification starts to show (not at once for some reason) but it goes away after some time, when the job is still running. The job itself doesn't get cancelled.
What does it all mean? Do I do something wrong? All I want is to launch long-running expedited job with a notification.
CodePudding user response:
Did you override getForegroundInfo()
in your Worker class? According to the docs:
Any ListenableWorker must implement the getForegroundInfo method if you would like to request that the task run as an expedited job
CodePudding user response:
If I explicitly call setForeground() in doWork() method (I don't know what is the difference), it slightly change the behavior:
https://developer.android.com/topic/libraries/architecture/workmanager/advanced/long-running
ListenableWorker now supports the setForegroundAsync() API, and CoroutineWorker supports a suspending setForeground() API. These APIs allow developers to specify that this WorkRequest is important (from a user perspective) or long-running.
On Android < 12 my work job worked fine: it is long-running and when it got started, the notification was shown in notification trey, and the job was done.
To maintain backwards compatibility for expedited jobs, WorkManager might run a foreground service on platform versions older than Android 12. Foreground services can display a notification to the user.
When targeting Android 12 or higher, foreground services remain available to you through the corresponding setForeground method.
https://developer.android.com/topic/libraries/architecture/workmanager/how-to/define-work#worker https://developer.android.com/static/images/guide/background/workmanager_main.svg
Workers don't know whether the work they're doing is expedited or not. But workers can display a notification on some versions of Android when a WorkRequest has been expedited.
To enable this, WorkManager provides the getForegroundInfoAsync() method, which you must implement so WorkManager can display a notification to start a ForegroundService for you where necessary.
Long story short - there is a lot of might, can, etc. What you need to do is debug the JobScheduler and figure out what exactly is happening and then try to figure out why.
- Check what exactly Constraint is satisfied or not.
- What is your Quota and Internet usage(you have an amount in milliseconds)
- What is your PowerBucket(you might ask the user to exempt you from battery optimization)
- History of executed jobs