Creating a Worker
Define background work in a Worker.
Creating a Worker is a free Android Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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()
}
}The Constructor
Every worker takes a Context and WorkerParameters in its constructor and passes them to the superclass. WorkManager instantiates your worker for you; you never call the constructor directly.
doWork Returns a Result
doWork() must return a Result describing the outcome. There are three possibilities.
Result.success()Result.retry()Result.failure()
Result.success
Return Result.success() when the work completed. The work is removed from the queue. You may attach output data to pass to chained work.
override suspend fun doWork(): Result {
uploadLogs()
return Result.success()
}Result.retry
Return Result.retry() for a transient failure, such as a temporary network error. WorkManager re-runs the worker later according to its backoff policy.
override suspend fun doWork(): Result {
return try {
syncData()
Result.success()
} catch (e: IOException) {
Result.retry()
}
}Result.failure
Return Result.failure() for a permanent failure that retrying will not fix, like invalid input. The work stops and dependent chained work will not run.
Reading Input Data
You pass small inputs via Data when enqueuing, and read them with inputData inside the worker. Keep it small; it is not for large payloads.
override suspend fun doWork(): Result {
val userId = inputData.getString("user_id") ?: return Result.failure()
sync(userId)
return Result.success()
}Returning Output Data
Workers can return Data in their result, which chained workers receive as their input. This is how you pass results between steps.
val output = workDataOf("count" to 42)
return Result.success(output)Work on the Right Dispatcher
CoroutineWorker runs doWork() on Dispatchers.Default by default. For blocking I/O, switch with withContext(Dispatchers.IO) to avoid starving the default pool.
override suspend fun doWork(): Result = withContext(Dispatchers.IO) {
readAndUploadFile()
Result.success()
}Keep Workers Self-Contained
A worker may run long after it was scheduled, in a fresh process. Do not rely on in-memory app state; pass what you need via input data or read from persistent storage.
Quick Check
Test your understanding of creating a Worker.
Recap
You created a Worker:
- Extend
CoroutineWorkerand override the suspenddoWork() - Return
Result.success(),Result.retry(), orResult.failure() - Read
inputDataand return outputData - Use
Dispatchers.IOfor blocking work; keep workers self-contained
Next: constraints and scheduling.
Frequently asked questions
Is the “Creating a Worker” lesson free?
Yes — the full text of “Creating a Worker” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.
What will I learn in “Creating a Worker”?
Define background work in a Worker. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Android Academy?
No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Creating a Worker” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Android Academy lesson?
Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.