Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WorkManager setRequiresDeviceIdle is confusing

I have implemented a scheduled work manager. My idea is to complete a process every 2 hours. But I need guaranteed execution. According to Work Manager's documentation every enqueued process will be executed guaranteed.

But now this setRequiresDeviceIdle is getting me confuse. It is stated in the documentation that by default setRequiresDeviceIdle has set to false. So what I assumed is that my process will not work if the device is in idle mode.

And Idle mode = When the phone is screen off for some interval.

But If I set this setRequiresDeviceIdle to true. I assume that now it will only work when device is in idle mode.

I want process to be complete even device is in idle or not in idle. What should I do now?

like image 653
Mudassir Zulfiqar Avatar asked Feb 18 '19 11:02

Mudassir Zulfiqar


People also ask

Is WorkManager deprecated?

This method is deprecated. Call getInstance instead.

Does WorkManager work when app is closed?

Android WorkManager is a background processing library which is used to execute background tasks which should run in a guaranteed way but not necessarily immediately. With WorkManager we can enqueue our background processing even when the app is not running and the device is rebooted for some reason.

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.

Can WorkManager be used to repeat jobs?

Retry and backoff policyIf you require that WorkManager retry your work, you can return Result. retry() from your worker. Your work is then rescheduled according to a backoff delay and backoff policy. Backoff delay specifies the minimum amount of time to wait before retrying your work after the first attempt.


1 Answers

If you go through the WorkManager Docs, you will find:

requiresDeviceIdle boolean: true if device must be idle for the work to run

If you pass true, it means that your work will be executed only when device is in idle state.

As you mentione you want your task to be executed always. Hence, you should pass false in setRequiresDeviceIdle().

Note: It's not necessary that your task will execute exactly after 2 hours. According to the DOCS, your task might be deferred till next maintenance window. You task would be executed for sure, but the duration won't be exactly 2hrs. It might be a little more than that.

In Doze mode, the system attempts to conserve battery by restricting apps' access to network and CPU-intensive services. It also prevents apps from accessing the network and defers their jobs, syncs, and standard alarms.

Periodically, the system exits Doze for a brief time to let apps complete their deferred activities. During this maintenance window, the system runs all pending syncs, jobs, and alarms, and lets apps access the network.

If you wan't your task to be always executed and at exact time, you can use Alarm Manager and setExactAndAllowWhileIdle(). But this practice is discouraged, as it is not good for battery performance.

like image 87
ankuranurag2 Avatar answered Nov 14 '22 07:11

ankuranurag2