Android WorkManager

Vinay
IVYMobility TechBytes
4 min readOct 20, 2021

WorkManager

WorkManager is an API that makes it easy to schedule reliable, asynchronous tasks that are expected to run even if the app exits or the device restarts. It is a battery-friendly API that encapsulates years of evolution of Android’s background behavior restrictions. This is critical for Android applications that need to execute background tasks!

It is intended for work that 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 or to periodically sync application data with a server.

But it is not intended for in-process background work that can safely be terminated if the app process goes away or for work that requires immediate execution.

Supporting from API 14, this API is a suitable and android docs recommended replacement for all the previous Android background scheduling APIs, including FirebaseJobDispatcher, GcmNetworkManager, and Job Scheduler.

WorkManager uses an underlying job dispatching service based on the following criteria:

As mentioned above, only the devices running on android API level 14 and above could support WorkManager, and the below won’t be compatible. Furthermore, API level 14 to 22 uses the existing GcmNetworkManager, AlarmManager and BroadcastReceiver for scheduling, and the later levels ply the JobScheduler.

Below is a portrayal for a better understanding:

Note: If the existing app targets Android 10 (API level 29) or above, the FirebaseJobDispatcher and GcmNetworkManager API calls included in the application will no longer work on devices running Android Marshmallow (6.0) and above. Migration has to be done based on the guidelines available on android docs.

Features

WorkManager also has a number of other key benefits, apart from providing a simpler and consistent API which includes:

  • Work Constraints
  • Robust Scheduling
  • Flexible Retry Policy
  • Work Chaining
  • Built-In Threading Interoperability

Lets dive a bit deeper with some real-time examples to comprehend the above mentioned features.

Work Constraints

We can define the optimal conditions for the work to run using Work Constraints. For example, run only when the device has internet connectivity or when it has sufficient storage space, etc.

Robust Scheduling

WorkManager provisions the developer to schedule the works to run one-time or periodically(repeatedly) using flexible scheduling windows available in the API. Also one can tag the work and name it as well, enabling to schedule unique, replaceable work and monitor or cancel groups of work together when required.

In a limelight, the scheduled works are stored in an internally managed SQLite database and the WorkManager ensures that the works persists and is rescheduled across device reboots. In addition, the API adheres to power-saving features like Doze mode, so the developer need not spend time on worrying on it. The API follows several other and best practices to optimize the energy and reduce bottlenecks too.

Flexible Retry Policy

There are always hard times. Even the scheduled work may fail due to several reasons. Behold, the WorkManager offers retry policies, including a configurable exponential back-off policy, so the work is never skipped and the data is never lost. The developers can be relieved of a great ache on the head.

Work Chaining

For any complex work, chain individual works can be chained together using an interface that allows us to control which among them run sequentially and which run in parallel.

Built-In Threading Interoperability

WorkManager integrates seamlessly with RxJava and Coroutines and provides the flexibility to plug in any custom asynchronous APIs.

Benefits

  • Handles compatibility with different OS versions by itself
  • Follows best practices to take care of system health
  • Supports one-time as well as periodic asynchronous tasks
  • Supports chained tasks with input/output
  • Lets you set constraints on when the task runs
  • Guarantees task execution, even if the app or device restarts

Why use WorkManager?

WorkManager runs background work while taking care of compatibility issues and best practices for battery and system health all by itself, without bothering the developer.

Furthermore, using WorkManager, we can schedule both periodic tasks and complex dependent chains of tasks: background work can be executed in parallel or sequentially, where you can specify an execution order. WorkManager seamlessly handles passing along input and output between tasks.

You can also set criteria on when the background task should run. For example, there’s no reason to make an HTTP request to a remote server if the device doesn’t have a network connection. So you can set a Constraint that the task can only run when a network connection is present.

As part of guaranteed execution, WorkManager takes care of persisting your work across device or application restarts. You can also easily define retry strategies if your work is stopped and you want to retry it later.

Finally, WorkManager lets you observe the state of the work request so that you can update your UI.

In the next article, lets look on how to work with the WorkManager and use it in the app.

To be continued…

--

--