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 ?
Use WorkManager for reliable workWorkManager is intended for work that is required to run reliably even if the user navigates off a screen, the app exits, or the device restarts. For example: Sending logs or analytics to backend services. Periodically syncing application data with a server.
IntentService + WakefulBroadcastReceiver + AlarmManager are deprecated with API 26 (Android 8.0 Oreo).
WorkManager is not answer to all of the background tasks. E.G. You shouldn't use it for processing payments since it doesn't need to survive process death and these task needs to be executed immediately. Consider using Foreground Service. Its also not a great idea to use them for parsing data and contents of view.
As mentioned in the previous section, WorkManager internally uses the JobScheduler API on Android 6.0 (API Level 23 – Marshmallow) and above devices. Therefore, there won't be any major differences between the WorkManager and JobScheduler on Android 6.0 and above devices. Both would behave in the same way.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With