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
-
I would say it depends on your exact use case. Can you elaborate more? And maybe also link to the documentation? – leonardkraemer Sep 19 '22 at 06:38
-
1Use alarm manager to start a task at specific time because workmanager sometimes starts after delay. If your network job is not large process you can use simple background task. – Kamal Sep 19 '22 at 07:43
-
if you need exact times you need `AlarmManager`. They say "To perform longer work, schedule it using `WorkManager` or `JobScheduler` from your alarm's `BroadcastReceiver`" in https://developer.android.com/training/scheduling/alarms#exact-system-resource-consumption - that probably applies to your use case if you need networking. But for notifications, I don't see why you'd need (exactly timed) networking, FCM push notifications exists so you don't need networking. And rarely executed network jobs can still schedule notification alarms at specific times. – zapl Sep 19 '22 at 16:54
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:
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)

- 1,363
- 1
- 10
- 22