Skip to content
DeveloperMemos

How to Use WorkManager in Android with Kotlin

WorkManager, Kotlin1 min read

WorkManager is a powerful library that allows developers to perform background tasks in an efficient and flexible way. It provides the ability to schedule tasks that run immediately or at a specific time, with support for constraints such as network availability or battery status.

In this tutorial, we will explore the basics of using WorkManager in an Android app written in Kotlin.

Adding WorkManager to Your Project

To use WorkManager in your Android project, you need to add the following dependency to your app's build.gradle file:

1dependencies {
2 implementation "androidx.work:work-runtime-ktx:2.7.0"
3}

Once you have added the dependency, you can start using WorkManager in your app.

Creating a Worker

A Worker is a class that performs a single background task. To create a Worker, you need to extend the Worker class and override the doWork() method. This method contains the code that will be executed when the Worker is scheduled to run.

Here's an example of a simple Worker that logs a message:

1class MyWorker(appContext: Context, params: WorkerParameters) :
2 Worker(appContext, params) {
3
4 override fun doWork(): Result {
5 Log.d("MyWorker", "Hello from MyWorker!")
6 return Result.success()
7 }
8}

In the doWork() method, we log a message using Android's Log class and return Result.success() to indicate that the work has been completed successfully.

Scheduling a Worker

To schedule a Worker, you need to create an instance of the PeriodicWorkRequest class and enqueue it using the WorkManager class.

Here's an example of scheduling the MyWorker class to run every 15 minutes:

1val myWorkRequest = PeriodicWorkRequestBuilder<MyWorker>(
2 15, TimeUnit.MINUTES
3).build()
4
5WorkManager.getInstance(context).enqueue(myWorkRequest)

In this code snippet, we create a PeriodicWorkRequest using the PeriodicWorkRequestBuilder class and specify that we want to run the MyWorker class every 15 minutes.

We then use the WorkManager.getInstance(context) method to get an instance of the WorkManager class and call the enqueue() method to schedule the myWorkRequest.

Cancelling a Worker

If you need to cancel a scheduled Worker, you can use the WorkManager.cancelWorkById() method and pass in the ID of the WorkRequest that you want to cancel.

Here's an example of cancelling a Worker:

1WorkManager.getInstance(context).cancelWorkById(myWorkRequest.id)

In this code snippet, we use the WorkManager.getInstance(context) method to get an instance of the WorkManager class and call the cancelWorkById() method to cancel the myWorkRequest.

Conclusion

In this tutorial, we learned the basics of using WorkManager in an Android app written in Kotlin. We created a simple Worker, scheduled it to run every 15 minutes, and cancelled it when we were done.

WorkManager is a powerful library that provides a flexible and efficient way of performing background tasks in an Android app. With its support for constraints and scheduling, it makes it easy to build apps that are responsive and provide a great user experience.