0Pricing
Android Academy · Lesson

Creating a Worker

Define background work in a Worker.

Creating a Worker

The unit of background work in WorkManager is a Worker. You define the task by subclassing a worker base class and overriding its work method.

CoroutineWorker

For Kotlin, extend CoroutineWorker. Its doWork() is a suspend function, so you can call other suspend functions and run on a background dispatcher naturally.

class UploadWorker(
    context: Context,
    params: WorkerParameters
) : CoroutineWorker(context, params) {
    override suspend fun doWork(): Result {
        // background work here
        return Result.success()
    }
}

All lessons in this course

  1. When to Use WorkManager
  2. Creating a Worker
  3. Constraints and Scheduling
  4. Periodic and Chained Work
← Back to Android Academy