1

I'm trying to run a task periodically every 12 hours, I'v used Work Manager and followed their documentation. The task is running periodically well as long as the user is actually using the app.

But if i close the app, or even just let my phone be idle for a while, it seems like the task stops working.

I searched for this problem on google, and came across posts such as this post, which i think explains the behaviour on my phone.

From my understanding, this problem is not related to work manager, but to all frameworks who will try to run background tasks? Is there a way to still periodically run tasks on most devices or is WorkManager is still the way to go?

Thanks!

Guy
  • 26
  • 5

1 Answers1

0

Please check this:

https://developer.android.com/topic/libraries/architecture/workmanager/how-to/debugging#use-alb-shell0dumpsys-jobscheduler

Required constraints: TIMING_DELAY CONNECTIVITY [0x90000000]
Satisfied constraints: DEVICE_NOT_DOZING BACKGROUND_NOT_RESTRICTED WITHIN_QUOTA [0x3400000]
Unsatisfied constraints: TIMING_DELAY CONNECTIVITY [0x90000000]
Minimum latency: +1h29m59s687ms
Run time: earliest=+38m29s834ms, latest=none, original latest=none

The periodic work is not exactly Periodic. You have different Constraints:

Explicit - you set them. Implicit - set by the system related to Battery optimization. You should save the battery and also Internet usage.

When you have a "periodic work" you actually have an explicit constraint called: TIMING_DELAY. But when the time is passed it does not mean that the work will start. It means that this constraint is satisfied and if and only all the other constraints are Satisfied - then the work will start.

And for example, you have your work with a "period" of 12 hours, but you wait an extra 4 hours for the other Constraints - you will have a period of 16 hours.

And after the work is finished - WorkManager will create a completely new job in the JobScheduler with TIMING_DELAY again - 12 hours. It will not account for the extra 4 hours. So you can't imagine something like:

I have 5 days so it means - 10 executions. It might be only 4 or 5 executions.

You can improve this by asking the user to exempt you from battery optimization:

https://developer.android.com/training/monitoring-device-state/doze-standby#support_for_other_use_cases

If you need to be really exact - you need to use AlarmManager, but mostly the idea of all of this is for the battery to be saved so it is not only about what the devs need, but also what the user needs.

Yavor Mitev
  • 1,363
  • 1
  • 10
  • 22
  • first of all, thanks for the comment! regarding the first part regarding the constraints, i know that, from what I'v seen in the documentation if you don't specify constraints, the default is having none(but thanks for pointing this out, and i didn't share the code and that could be the cause for my problem). and ok, got this, i think it will be fine if my task will not run in case the user kills the app, i would still want to change this, so i will be super happy if anyone knows if its possible to still run the task on most devices, but i guess its not that bad :) – Guy Sep 12 '22 at 11:22