'Android recommended and reliable API for periodic background work?

I've been using WorkManager to create notifications for my app. For my purposes I figured PeriodicWorkRequest is the most fitting, but after a bit of testing and reading online it's seems extremely unreliable. Using the minimal interval (15 minutes), and the app being closed, the worker woke up 5-6 times and then seems to be killed.

So how does one go about creating background work that wakes up in reasonable time intervals? What is the best approach for creating event-based notification? My idea was checking for the event (for example, checking for something new in the database) in small time intervals (with 15 minutes also being less than ideal), but seeing as it doesn't work well with PeriodicWorkRequest and is also the recommended approach as per the documentation, what exactly are my options?



Solution 1:[1]

Basically, the idea of Android is for you not to be able to do what you want to do because we as developers try to kill the battery.

You need to see how the evolution of the restrictions goes:

Version 6 - Doze:

https://developer.android.com/training/monitoring-device-state/doze-standby https://developer.android.com/about/versions/marshmallow/android-6.0-changes#behavior-power

Version 7 Another state of Doze with even more restrictions: https://developer.android.com/about/versions/nougat/android-7.0-changes#perf

Broadcast Restrictions: https://developer.android.com/guide/components/broadcasts https://developer.android.com/about/versions/nougat/android-7.0-changes#bg-opt

Version 8.0 Background execution limits: https://developer.android.com/about/versions/oreo/background#services

Version 9 StandBy Buckets - where depending on how the app is used you have different resources to use - like time to wake up the app, time to use the Network, etc

https://developer.android.com/about/versions/pie/power#buckets https://developer.android.com/about/versions/12/behavior-changes-all#restrictive-app-standby-bucket https://developer.android.com/topic/performance/appstandby

Battery Save improvements:

https://developer.android.com/about/versions/pie/power#battery-saver

Power Management Restrictions - really important. https://developer.android.com/topic/performance/power/power-details

Version 11 and 12 App hibernation

https://developer.android.com/topic/performance/app-hibernation

Long story short - you need to prevent all these restrictions to harm your work. But you need to comply because it is better for the user.

But there is no API that will just say - "f**k all these restrictions and do whatever the dev wants to do."

If you need exact timing - you need AlarmManager.

If you do not know when you need to do your work and depend on the outside - Push Notifications which then can transfer the work to the WorkManager.

If you need periodic work that is not time-critical - you might not use the AlarmMangaer and be sure that the work is finished, but you can't be sure when, because there are many restrictions and the priority will be saving the resources.

Also, you can ask the user to be exempted from Battery Optimization:

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

If you want to know why exactly the work is not executed you need to check the JS dump and see what restriction is not satisfied:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yavor Mitev