Which is better alarm manager or workmanager

Viewed 33

I want to show notification at certain time and day by using retrofit2 networking. But in alarm manager doc. It mentioned that isn't proper fpr networkong job. But in workmanager doc it says for the exact time job use alarm manager. What would be better for me to use? Alarm manager nor work manager

1 Answers

Android has introduced so many things that need to save battery life like:

Doze version 1 - API 6.0 Doze version 2 - API 7.0

https://developer.android.com/about/versions/nougat/android-7.0-changes#perf

Removing CONNECTIVITY_CHANGE Manifest declaration and others in - API 7.0

https://developer.android.com/guide/components/broadcasts

Background execution limitations in API 8.0

https://developer.android.com/about/versions/oreo/background#services

App Standby Buckets API 9.0 (Upgraded in Android 12)

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 Saver improvements in API 9.0

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

App Hibernation in 11 and 12 API.

All this stuff were not done for us to be able to do whatever we want by using the WorkManager. WorkManager was created to integrate all this requirements in a single API.

That is why the idea behind it is:

  • do some work
  • finish it at some point for sure(even if the device is restarted)
  • SAVE THE BATTERY AND NETWORK USAGE BY COMPLYING WITH THE ABOVE

Before that you had the AlarmManager to wake up in exact time

Services to keep the app process alive by giving your application priority. You still have them with the exclusion that now the starting services in the background are problematic. As per:

https://developer.android.com/about/versions/oreo/background#services

So if you want to do things like this you need to fully understand the Android API and limitations.

If you need help in StackOverflow - you need to fully understand your use case, you should have really put your mind into trying to understand it, communicate all these limitations with your PMs and explain to them that the Android Ecosystem is pretty complicated, there are a lot more applications besides the one you are writing and they all compete for resources. We can't do everything that we want. It is highly unlikely for the user itself to want to do long-running operations in exact timing starting from the background. But it might need it. Also, you have the option to ask to be exempted from battery optimization:

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

Also, some people try to use Push notifications, but it is highly unlikely that it will work in your case. You can send high-priority ones, but if android detects that the user is not interacting with the notification they will lose its priority. And if you do not have the priority and also want to do a long-running operation - you need to create... a Work in the WorkManager :)

But long story short:

what will wake you up:

  • AlarmManager is exact
  • WorkManager is not

what will keep you alive:

  • Service
  • WorkManager(it uses its own service)
Related