WorkManager for Kotlin: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to WorkManager for Kotlin

\n\n

WorkManager is a library from Google for Android, designed for executing background tasks that must be guaranteed to complete even after a device reboot or app closure. It is part of Android Jetpack and provides a simple and reliable API for scheduling deferred, periodic, and one-time tasks.

\n\n

The main goal of WorkManager is to replace outdated background work mechanisms such as Service or AsyncTask and ensure compatibility with various Android versions (from API 14+). The library automatically selects the optimal way to execute a task: JobScheduler (API 23+), AlarmManager + BroadcastReceiver (older versions), or Firebase JobDispatcher (if connected).

\n\n

WorkManager is ideal for scenarios such as: syncing data with a server, sending analytics, downloading files, image processing, backups, sending push notifications, and any other tasks that do not require immediate execution but must be completed in the near future.

\n\n

Installing WorkManager

\n\n

WorkManager is not installed via pip or npm, as it is a library for Android (Kotlin/Java). To add it to your project, you need to modify the build.gradle file (module level).

\n\n

Step 1: Open your module's build.gradle file (app/build.gradle).

\n\n

Step 2: Add the dependency in the dependencies block:

\n\n
dependencies {\n    implementation "androidx.work:work-runtime-ktx:2.9.0"\n}
\n\n

Step 3: Sync the project (Sync Now).

\n\n

If you are using Kotlin DSL, the dependency will look like this:

\n\n
dependencies {\n    implementation("androidx.work:work-runtime-ktx:2.9.0")\n}
\n\n

Important: Version 2.9.0 is the current version at the time of writing. It is recommended to check the latest version on the official Android Developers website.

\n\n

Quick Start with Code Example

\n\n

Let's look at a simple example: creating a background task that prints a log to Logcat.

\n\n

Step 1: Create a Worker class

\n\n
import android.content.Context\nimport android.util.Log\nimport androidx.work.Worker\nimport androidx.work.WorkerParameters\n\nclass SimpleWorker(context: Context, params: WorkerParameters) : Worker(context, params) {\n\n    override fun doWork(): Result {\n        Log.d("SimpleWorker", "Background task completed!")\n        // You can perform any long-running operation here\n        return Result.success()\n    }\n}
\n\n

Step 2: Schedule the task execution

\n\n
import androidx.work.OneTimeWorkRequestBuilder\nimport androidx.work.WorkManager\n\nval workRequest = OneTimeWorkRequestBuilder<SimpleWorker>()\n    .build()\n\nWorkManager.getInstance(context).enqueue(workRequest)
\n\n

After calling enqueue(), the task will be executed in the near future (usually within a few seconds, if there are no constraints).

\n\n

Key Methods and Functions of WorkManager

\n\n

1. OneTimeWorkRequestBuilder

\n

What it does: Creates a one-time request to execute a task. The task runs once and is then removed from the queue.

\n

Parameters:

\n
    \n
  • workerClass (required) — a class extending Worker or CoroutineWorker.
  • \n
  • setInitialDelay() — delay before execution (in milliseconds).
  • \n
  • setConstraints() — constraints (e.g., internet connection).
  • \n
  • addTag() — tag for grouping tasks.
  • \n
  • setInputData() — input data for the Worker.
  • \n
\n

Recommendations