WorkManager not running when app is closed / killed
Asked Answered
N

2

7

I am trying to run a simple work every 10 seconds using WorkManager. It works perfectly when app is running on background or foreground. When I close the app (kill the app), the work will not be called anymore.

I call the below code when MainActivity created

fun scheduleNotification(context: Context) {
    val workRequest = OneTimeWorkRequest.Builder(NotificationWorker::class.java).setInitialDelay(10000, TimeUnit.MILLISECONDS)
    WorkManager.getInstance().enqueueUniqueWork("NotificationWorker", ExistingWorkPolicy.REPLACE, workRequest.build())
    Log.d("NotificationWorker", "start")
}

The Worker class

class NotificationWorker(context: Context, params: WorkerParameters) : Worker(context, params) {

override fun doWork(): Result {
    Log.d("NotificationWorker", "Working")
    NotificationUtil.scheduleNotification(applicationContext) // n times
    return Result.success()
}
}

These code work well when my app does not closed.

How can I make it runs even when the app was closed?

Nik answered 31/5, 2019 at 4:52 Comment(19)
work manager has 15 minutes minimum to run in periodic manner, change to 15 mins and run the code againCogen
Instead of OneTimeWorkRequest you have to use PeriodicWorkRequest for repeating any task. for more check this out :- developer.android.com/topic/libraries/architecture/workmanager/…Foudroyant
On which device, OS version are you testing this? Can you reproduce the issue on the emulator with the standard Android image?Bomke
Thank you guys for help. @pfmaggj I tested on OnePlus 6, Android 9. Actually, the same code works on my other projects which implemented Downloader Library to download apk expansion files. I am not sure if I need to implement some service to make the app wake lockNik
Would be interesting to see if the code works on another device with a vanilla android (like the emulator). Could you please open a bug on the WorkManager's issuetracker?Bomke
Weird. It works on the simulators with the same OS version but my real OnePlus 6 phone.Nik
Have you found a solution for this? I am facing the same issue.Mouthpiece
Be it one time work request or a periodic request, when the app is closed, the worker does not run. I tried it with my Oppo and Pixel 2 emulator but no luck. But it runs on a Samsung (I tried the Galaxy S10+). When I create a foreground service, that survives in the emulator and galaxy but oppo kills that too!!Hearsay
This issue is faced for me in only android 10 OS. Did anyone got any advice?Sty
@GoldChicken You mentioned you want it to run "every 10 seconds" and I see you are using OneTimeRequest. I know PeriodicWorkRequest won't work for this. But how are you scheduling it for "every 10 seconds"?Carvel
@PrajwalWaingankar Have you got any solution on it.I have also stuck in the same.Please help me on itDiscontinue
@AnirudhGanesh Have you got any solution on it.I have also stuck in the same.Please help me on itDiscontinue
For me the issue was that I was calling a task in the dowork() of workmanager on a non ui thread where the task was ui based and so hd to be performed on the main thread. @RavindraKushwahaSty
Thanks for the input!!!! In my case it does not run in background , but when i launch the app then it run..Can you please guide on it? @PrajwalWaingankarDiscontinue
What is the task you r running in dowork()? Also r u using handler in that function called in dowork()?Sty
@RavindraKushwaha What device are you using to test? Chinese devices have this problem of killing of bg work when the apps are swiped from the recent screen. So you need to get battery ignore optimization for the Chinese for but then you won't be able to publish the app in PlayStore. So you have to show the user how to navigate to settings and turn off battery optimizations for the app or let it run in the bg instead of restricting it. or give a foreground notification and ask the user not to swipe the app from recent. This will work up to some extent.Hearsay
Thanks for the suggestion!!!!! I am using the MI phone, means problem is in it , what should be do for it, is there not any otherway? Again thanks dost :)Discontinue
@PrajwalWaingankar I am just writing the log in text file after the 15 minutes.Discontinue
Pls post the code for the same here once. Add a code snippet of your dowork()Sty
A
2

Periodic works Work manager only work at minimum interval of 15 minutes. If you need to do work continuously then use foreground service to define your work. Remember to put it in separate thread and a foreground service notification to keep it alive otherwise system will kill it within a minute.

Aircraft answered 29/5, 2021 at 13:4 Comment(0)
G
-3

I would suggest you to use the PeriodicWorkRequest for such tasks that need to execute periodically. Check the details here.

https://developer.android.com/topic/libraries/architecture/workmanager/how-to/recurring-work

Gossipy answered 31/5, 2019 at 5:22 Comment(1)
You cannot use a PeriodicWorkRequest in this case because the minimum interval is 15 minutes.Bomke

© 2022 - 2024 — McMap. All rights reserved.