Use WorkManager for Android Background Processing

Jan 22, 2019

Why use WorkManager?

If WorkManager executes one of your tasks while the app is running, WorkManager can run your task in a new thread in your app's process.

If your app is not running, WorkManager chooses an appropriate way to schedule a background task--depending on the device API level and included dependencies, WorkManager might use JobScheduler, Firebase JobDispatcher, or AlarmManager.

It does comes with a few nice features:

  • Ability to schedule one time or periodic request.
  • Ability to put on constraint, such as only run when network is available, or don't run when battery is low.
  • Support for a retry policy.
  • Support to ensure only one unique instance of the task is running.
  • LiveData support to get task updates.
  • Chain multiple tasks together.


WorkManager classes are already in the package, but currently depend on Support Library 27.1, and associated Arch component versions. Version of WorkManager with AndroidX dependencies will be released in the future.

WorkManager requires compileSdk version 28 or higher.

def work_version = "1.0.0-beta02"
// use -ktx for Kotlin+Coroutines
implementation "$work_version"

// optional - RxJava2 support
// implementation "$work_version"

// optional - Test helpers
// androidTestImplementation "$work_version"

NOTE: Refer available dependencies.


class BackupWorker(context : Context, params : WorkerParameters)    : Worker(context, params) {    companion object {        fun run() : LiveData<WorkInfo> {            val work = OneTimeWorkRequestBuilder<BackupWorker>().build()            WorkManager.getInstance().enqueue(work)            return WorkManager.getInstance().getWorkInfoByIdLiveData(        }    }    override fun doWork(): Result {                return Result.success()    }}


❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.