WorkManager vs AlarmManager, what to use depending on the case
Asked Answered
A

1

48

I have to perform this use case (not code , just the right usage) The use case : I need to fetch some data from the network everyday at 00:30 . These data provide me some specific times , and one of them is around 4:30 (changes everyday by +1 minute -1 minute , depends on the server response, can't use ++ or -- logic anywhere) . On this one (4:30), I need to schedule an Alarm . What is unclear :

Should I use AlarmManager directly for this ?

Should I use WorkManager to get the time when I need to alarm and than use AlarmManager ?

Should I just use WorkManager ?

The reason why I am confused is because some blogs I have read say that is better to stick to AlarmManager if I have some work at a specific time, but still, I can do it with WorkManager

So how is this done ?

Aplanatic answered 10/5, 2019 at 9:14 Comment(0)
S
29

Should I use AlarmManager directly for this ?

Yes you should. AlarmManager is the best option as far as I know to handle tasks like yours and also is the safer option when dealing with doze mode. Use the first alarm to set the second alarm at a specific time.

Should I use WorkManager to get the time when I need to alarm and than use AlarmManager ?

If you want to use this approach you need to call AlarmManager and dispatch a Worker on WorkManager. The WorkManager need to run before a specific time and is not guaranteed that the Worker finish or will be executed before 4.30.

The reason why I am confused is because some blogs I have read say that is better to stick to AlarmManager if I have some work at a specific time, but still, I can do it with WorkManager

WorkManager doesn't guarantee the time of execution. It probably can do this in the future.

Should I just use WorkManager ?

No, for the reasons expressed before. Android-Job is the short term response of your use case if you wanna use a job scheduler. Also you can see a table of features and differences if you go to the link.

Edit 17/03/2020: Android-Job is deprecated.

Edit 29/03/2023: For other use cases check the paragraph Acceptable use cases for exemption on documentation.

Sansone answered 10/5, 2019 at 15:3 Comment(7)
I have used alarm manager to fire an alarm and open activity A. It is works. But when I Reschedule the alarm after 5 min It is not firing any more. What should the problem? Can you please help me?Width
Are you setting the Alarm manager parameters correctly? Are you sure you are rescheduling the alarm correctly? Which version of Android are you using to test? Behavihor and rules for alarms are strictly related to android versions, similar to background processing. Open a question and give an example or some snippets of your code. Check the docs also on: developer.android.com/training/scheduling/alarmsSansone
I have passed all parameters correctly. I am testing it on 7.1 and The problem is doze mode. When application is killed my app is not firing the alarm.Width
WorkManager sit on top of few api's, one of them is AlarmManager, why WorkManager on top of AlarmManager is not enough?Bombastic
It depends on what you want to achieve. The WorkManager seem to not be implemented to execute tasks at exact time (I think this feature will be implemented in the future). If you go to developer.android.com/guide/background , the recommended way to execute exact time jobs is the AlarmManager, and for all the other use cases you can use WorkManager. There are more informations here developer.android.com/topic/libraries/architecture/workmanager. You can try to create a simple app to test these differences (the behavihor is slighty different depending on android version)Sansone
@ChiragPrajapati check here #28140719 . Probably you need to do some other modification if you use an android version that is newer than Android 8 (e.g. Foreground services). Hope this help.Sansone
as of api v26 workmanager works are ignored when device is in doze mod. see developer.android.com/training/monitoring-device-state/…Ziegfeld

© 2022 - 2024 — McMap. All rights reserved.