Introduction to Android WorkManager

The WorkManager API is a replacement for all background scheduling APIs. WorkManager incorporates the features of its predecessors in a modern, consistent API that works back to API level 14 while being conscious of battery life.

WorkManager makes it easy to schedule deferrable, asynchronous tasks that are required to run even if the app is killed or device is restarted when the work constraints are satisfied.

WorkManager allows us to observe the status of the scheduled work. It also allows us to create complex chain of deferrable work.

It uses an underlying job dispatching service based on the below criteria:

  • If the API level is 23+, then it uses JobScheduler
  • else if the API level is 14–22, it uses a custom implementation of AlarmManager and Broadcaster Receiver.

All background work is given ten minutes to complete. Post ten minutes, the Worker is signalled to stop.

When to use WorkManager ?

This blog post about modern background execution captures when in detail when to use WorkManager.

WorkManager can be paired with other APIs when you need to trigger some background work in more complex scenarios:

  • If your server triggers the work, WorkManager can be paired with Firebase Cloud Messaging.
  • If you are listening for broadcasts using a broadcast receiver and then need to trigger long running work, you can use WorkManager. Note that WorkManager provides support for many common Constraints that normally come through as broadcasts — in these cases, you don’t need to register your own broadcast receivers.

Using WorkManager, we can schedule both one off and repeating tasks. WorkManager runs background work while taking care of compatibility issues, follows best practices for battery and system health.

Underneath, WorkManager uses the right job dispatching service based on the API level.

Summary

  • WorkManager lets you observe the state of your Work
  • Takes care of compatibility issues because of different OS versions
  • Follows power and system health best practices
  • Supports asynchronous one-off and repeatable tasks
  • Supports chained tasks with input and output
  • Invokes work when the set constraints are satisfied
  • Makes sure that the work request executes even if the app is killed or device is restarted.

Next

We will look into an example implementation of background work using WorkManager

References