Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Work Manager vs Services?

In my Android app i have multiple intent services which run one after another and the very first intent service is trigerred by a broadcast. I came across Work Manager a few days ago and really liked the simplicity of the Worker and the WorkManager classes. What are the pros and cons of Work Manager over regular intent services? Should i switch to work manager now considering the fact that i may have to write more intent services in the future? Also which option would help me test the code easily?

like image 967
Dishonered Avatar asked May 15 '18 06:05

Dishonered


People also ask

What is difference between WorkManager and service in Android?

WorkManager provides a better way of handling background tasks. Runs on devices with/without Google play services. No need to worry about threading as WorkManager will ensure execution on the background thread. Easy to schedule, cancel, retry & query the work.

What is Android Work Manager?

WorkManager 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.

What is the difference between job scheduler and WorkManager in Android?

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.

What is Work Manager used for?

Android WorkManager is a background processing library which is used to execute background tasks which should run in a guaranteed way but not necessarily immediately. With WorkManager we can enqueue our background processing even when the app is not running and the device is rebooted for some reason.


1 Answers

WorkManager comes with following features:

  • Provides tasks which can survive process death
  • It can waken up the app and app's process to do the work thereby guarantees that works will be executed.
  • Allows observation of work status and the ability to create complex chains of work
  • Allows work chaining which allows to segregate big chunk of work into small works and execute them based on different constraints
  • Gracefully manages doze mode or other restrictions imposed by OS.

Following would be the cases where it would be helpful:

  • Executing long running background tasks like uploading media
  • Parsing and storing data in database.
  • Critical Tasks which needs to survive process deaths

Should i switch to work manager now considering the fact that i may have to write more intent services in the future?

In most cases it should be replacement for IntentService but you have to consider carefully before using it. It could be that IntentService was not the best choice at first place.

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.

You really need to weigh whether you need capabilities of it before using it. Since Google is almost re-vamping the way we code, WorkManager would be solution of our Background processing woes. For sure it would be most important option since it abstracts handling of several constraints imposed by OS. You should consider using it for future implementations.

Also which option would help me test the code easily?

Google has also provided testing library which facilitates WorkManager's test at ease. Its still under development but should become more powerful before its released.

like image 69
Sagar Avatar answered Sep 27 '22 21:09

Sagar