Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Work Manager not working when app is killed by the user. Why?

I want to execute a task, even after the application is killed using work manager. But, the task is not executed after the app is killed.

    workManager = WorkManager.getInstance();
    Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
    OneTimeWorkRequest saveData = new OneTimeWorkRequest.Builder(SaveDataWorker.class).setConstraints(constraints).setInitialDelay(10,TimeUnit.SECONDS).build();
    workManager.enqueue(saveData);
like image 273
minato Avatar asked Mar 26 '19 03:03

minato


People also ask

Does Work Manager work when app is killed?

WorkManager is not intended for in-process background work that can safely be terminated if the app process goes away.

Which method is called when app is killed android?

When Android decides to kill our app, our activities will call onDestroy method.

When should I use work manager?

Earlier running background tasks was a very tough task in Android but now with the help of Work Manager, we can schedule our tasks easily. Work Manager is a library of Android Jetpack. It allows the app to do things in the background even when the app is exited or the device is restarted.

How do I stop WorkManager?

Stop a running Worker You explicitly asked for it to be cancelled (by calling WorkManager. cancelWorkById(UUID) , for example). In the case of unique work, you explicitly enqueued a new WorkRequest with an ExistingWorkPolicy of REPLACE . The old WorkRequest is immediately considered cancelled.


1 Answers

As I found out, the work manager depends on the device manufacturer. In my case, it is an miui device, which does not allow work manager to work in case the app is killed or rebooted. The work manager worked when I provided the application with "autostart permission".

like image 117
minato Avatar answered Oct 02 '22 08:10

minato