Android Work Manager

Ali Göktaş
2 min readJan 20, 2023

--

Background tasks are an essential part of any Android app, allowing developers to perform actions such as sending data to a server or cleaning up local storage without interrupting the user experience. However, scheduling these tasks can be a tricky task, as developers must balance the need to perform the task with the need to conserve battery power.

Enter Android Work Manager, a library that makes it easy to schedule and execute background tasks in a way that is optimized for power consumption. In this article, we’ll take a look at how to implement both OneTime and Periodic Work Manager tasks, and how to customize it to fit the needs of your app, using Kotlin as the programming language.

First, let’s start by adding the Work Manager dependency to our app’s build.gradle file:

implementation "androidx.work:work-runtime:2.4.0"

Next, we’ll create a new class that extends the Worker class. This class will contain the code for our background task. For example, here's a simple worker that sends a message to a server:

import androidx.work.Worker

class SendMessageWorker(
context: Context,
params: WorkerParameters
) : Worker(context, params) {

override fun doWork(): Result {
// Code to send message to server
return Result.success()
}
}

Now that we have our worker class, we can schedule it using the Work Manager.

First we will look into scheduling a one time task:

import androidx.work.OneTimeWorkRequest

val sendMessageWork = OneTimeWorkRequestBuilder<SendMessageWorker>().build()
WorkManager.getInstance(this).enqueue(sendMessageWork)

This is a basic example of how to use Android Work Manager to schedule a one time background task.

Next, we will look into scheduling a periodic task:

import androidx.work.PeriodicWorkRequest
import java.util.concurrent.TimeUnit

val sendMessageWork = PeriodicWorkRequestBuilder<SendMessageWorker>(15, TimeUnit.MINUTES).build()
WorkManager.getInstance(this).enqueue(sendMessageWork)

As you can see, we are using PeriodicWorkRequestBuilder class to create the periodic work request, passing in the SendMessageWorker class and the repeat interval of 15 minutes. It's worth noting that, unlike OneTimeWorkRequest, PeriodicWorkRequest will automatically reschedule itself after each execution, so you don't have to enqueue it multiple times.

Both of these examples are basic implementations, but Work Manager offers many more features that can be used to customize your tasks to fit the needs of your app. For example, you can chain multiple tasks together, schedule tasks to run at specific intervals, or set constraints on when the task should run like network connectivity, charging etc.

In conclusion, Android Work Manager is a powerful and easy-to-use library for scheduling background tasks on Android. It allows developers to perform tasks in the background without sacrificing battery life, and provides a wealth of tools and APIs for customizing tasks to fit the needs of your app. Give it a try in your next project and see how it can simplify your background task management.

Thank you for your time and please feel free to directly reach me in case of having any question or suggestion. 😇 😇

https://www.linkedin.com/in/aligkts/

--

--