- Tram Ho
The release of Android Jetpack brings a lot of new and useful features to developers. In it, WorkManager is an API that makes it easy to schedule and run asynchronous tasks even if the app is quit or the device is restarted. It is recommended as a replacement for previous Android scheduling APIs such as FirebaseJobDispatcher, GcmNetworkManager, and Job Scheduler. So what exactly is it, what’s good, let’s find out in this article!!!
1. What is WorkManager?
As mentioned, WorkManager is an API in the Android Jetpack which is very popular and makes scheduling possible. More specifically, it is an Android library that runs the intended job when the job’s condition is satisfied (when there is a network, at some point, …).
WorkManager provides an efficient battery-saving API, optimized for different Android versions
a. Operating conditions
It is possible to decide when a job is run, using Work Constraints. For example, run only when the device is connected to Wi-Fi, when the device is idle, when there is enough storage space, …
b. Powerful scheduling
WorkManager allows one-time or repeated scheduling. Tasks can be tagged or named, making it easy to manage one to many tasks. Jobs are stored in an internal SQLite database. WorkManager makes sure it works fine even if the device is rebooted. It complies with energy-saving features like Doze mode, so you don’t have to worry.
c. Flexible retry capabilities
Sometimes the job runs in error. WorkManager provides flexible retry capabilities, including redundancy.
d. Work series
WorkManager helps link tasks together with a simple, easy-to-understand code. For example
Basic reading can be understood immediately. Start with job A, B. Then continue with C. Or similar :3
For each task, it is possible to define input and output for it. And when in a workflow, the output of one is automatically passed by WorkManager to the next one Good, no need to deal with clutter.
e. Stream interoperability
WorkManager is easily integrated with RxJava and Coroutines, providing powerful asynchronous task execution.
So when to use WorkManager. Since there are many other Android libraries that can also run similar to Alarm Manager, Sync Adapter, …
The answer is work “has to finish and is deferrable”
- Has to finish: when the app is closed by the user, is it necessary to get the job done (Has to finish)? For example, you want to sync data to the server when the job is done. This should happen even if you switch to another app or the app is closed for more memory. WorkManager will help you to solve it.
- Is deferrable: If the task needs to run after a certain amount of time, it is a deferred task (Is deferrable) . Let’s say you want to schedule a job to run in 3 hours. Then you can use WorkManager to do this
Details of usage you can see here: https://android-developers.googleblog.com/2018/10/modern-background-execution-in-android.html
WorkManager support from API 14 (Android 4.0, released in 2011). You can see it’s very old already. To do this, WorkManager use JobScheduler or a combination of BroadcastReceiver and AlarmManager
The Android Doc has complete instructions, you should check it out at https://developer.android.com/topic/libraries/architecture/workmanager/basics
WorkManager is an API that makes it easy to schedule jobs, with many useful features. What are you waiting for if you don’t use it?
You can read more about Android, listen to gossip at my blog Code with Trung
Link blog: https://codecungtrung.com/