WorkManager setRequiresDeviceIdle is confusing
Asked Answered
I

1

10

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?

Intrinsic answered 18/2, 2019 at 11:3 Comment(6)
Default constraint for this method is false meaning that your work will run particularly, passing true will restrict that condition. So, once ture, means work will only run in idle state.Saturninasaturnine
That is pretty much okay to me, My problem is to run in every condition? in idle and not in idle. How to achieve that behaviour?Intrinsic
Where have you read that "Idle mode = When the phone is screen off for some interval." ?Escuage
@androiddeveloper we see a lot of logs about idle mode = true when people are not using their phones usually overnight.Intrinsic
@MudassirZulfiqar You checked there that the screen is turned off?Escuage
@androiddeveloper yes phones were locked and screens were off.Intrinsic
S
4

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.

Stereograph answered 20/2, 2019 at 10:35 Comment(5)
Apologies, but I'm reading mixed messages from your response. In the first section, I get the sense that the worker would run while the device is in Doze mode ("executed only when device is in idle state"). But in the second section, I'm getting the sense that it will only run in maintenance windows. Hence, let's say I wanted my periodic worker to run roughly every 15 minutes while it's in Doze mode. I'm unable to understand if this is possible or not with setRequiresDeviceIdle()Chuvash
@JHowzersetRequiresDeviceIdle() is totally different from doze mode. They are not related. If you set setRequiresDeviceIdle(true), then the task would be executed only when device is idle. While setRequiresDeviceIdle(false) can will execute the task even in case the app is running or not in standby. “idle” is considered to be the time in which Doze Mode is activeStereograph
Coming back to your question, your use case is possible with setRequiresDeviceIdle(). Only things is that the periodic time won't be exactly 15 minutes. The maintenance window is a part of doze mode, where all the deferred task are bundled together and executed.Stereograph
This also confuses me. To me, setRequiresDeviceIdle(true) forces a conflict of interest: it is saying that you want the job to run only when the device has decided that it is time for it to go into an idle state... when it does not want to be performing any work?Agnesse
When exactly is a device idle? What is the definition?Tercel

© 2022 - 2024 — McMap. All rights reserved.