• 5

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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?

      • 1
    • 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.
      • 2
    • 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?

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.

  • 1
Reply Report
      • 2
    • 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()
    • @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 active
      • 1
    • 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.
      • 1
    • 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?