Home » Android » android – WorkManager vs AlarmManager, what to use depending on the case

android – WorkManager vs AlarmManager, what to use depending on the case

Posted by: admin June 15, 2020 Leave a comment

Questions:

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 ?

How to&Answers:

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.