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);
WorkManager is not intended for in-process background work that can safely be terminated if the app process goes away.
When Android decides to kill our app, our activities will call onDestroy method.
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.
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.
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With