Solution 1 :
Work Manager is Deferrable background work when the
work’s constraints are satisfied. if all the constraints set on a WorkRequest are
satisfied, Work can still be run with some additional delay. It’s because WorkManager respecting Android battery optimization strategies. Also, you can read more about WorkManager constraint to understanding the work’s constraint here:
I have an app that uses both periodic and onetime workers to perform various tasks, some of which require a network connection. My client reported to me that a certain required daily task was not performed on a day he did not use the app at all. Despite the app’s not having been run on a given day, the workers still need to run in order to report various statistics to the backend server. I assume this is due to the fact that the app was in App Standby Mode for the entire day, as well as the device’s being in Doze Mode for at least part of the day. So my question is, is it possible to make a periodic or onetime worker run while the app is in App Standby or Doze Mode, or will it always be delayed until the device and/or app wakes up?
Alternatively, would I be better off using alarms instead of workers? I see that it is possible to set an alarm using
setExactAndAllowWhileIdle() that “will be allowed to execute even when the system is in low-power idle modes.” Does “idle mode” imply Doze and/or App standby mode?
I have been grappling with this for a while now, so any help and/or guidance is greatly appreciated.
Comment posted by Yabaze Cool
By using alarm Manager or Job Scheduler you can achieve this. Before that, you have to disable “Doze Mode”. To disable Doze Mode, You have to ignore Battery Optimization. Reference:
Comment posted by Peter Jacobs
Thank you. I will look into this solution. However, does adding the permission REQUEST_IGNORE_BATTERY_OPTIMIZATIONS to the Android manifest require user approval? If so, I suspect some users might be wary about allowing an app to ignore battery optimizations. While I would like these workers to run at the time they are scheduled, despite doze mode, I do not want the app to be a battery hog. That said, these workers are relatively lightweight and only need to be run once every 24 hours.