WorkManager in Android

Yusuf Gültaç
2 min readJul 27, 2021

In this article, you will learn how to use WorkManager to schedule a background tasks in an efficient way.

Processing data in the background is an important part of creating an Android application. For example, an app might upload files to a server, sync data from a server and save it to a database or execute expensive operations on data. Such operations should be performed in the background, off the main thread where most of your application code is run. Background tasks consume a device's limited resources, like RAM and battery. This may result in a poor experience for the user if not handled correctly.

WorkManager is one of the Android Architecture Components and part of Android Jetpack.

WorkManager is an API that makes it easy to schedule asynchronous tasks that are expected to run even if the app exits or the device restarts. WorkManager also lets you set criteria on when the background task runs. For example, you might want the task to run only when the battery status, network status, or charge state meet certain criteria.

Let’s familiarize ourselves with WorkManager library. Operation take place in 3 main classes

1.Worker:

This class is where you define the actual work (the task) to run in the background. You extend this class and override the doWork() method. The doWork() method is where you put code to be performed in the background, such as syncing data with the server or processing images. You implement the Worker in this task.

doWork() method performs work synchronously, and should return a ListenableWorker.Result object. The Android system gives a Worker a maximum of 10 minutes to finish its execution and return a ListenableWorker.Result object. After this time expired, the system forcefully stops Worker.

Result.success()—work completed successfully.

Result.failure()—work completed with a permanent failure.

Result.retry()—work encountered a transient failure and should be retried.

2.WorkRequest:

This class represents a request to run the worker in background. Use WorkRequest to configure how and when to run the worker task, with the help of Constraints such as device plugged in or Wi-Fi connected. There are two concrete implemantations of the Workrequest class.

The OneTimeRequest class is for one-off task.

The PeriodicWorkRequest class is for periodic work, work that’s repeasts at intervals.

3.WorkManager:

This class schedules and runs your WorkRequest. WorkManager schedules work requests in a way that spreads out the load on system resources, while honoring the constraints that you specify.

Conclusion

The WorkManager API makes it easy to schedule deferrable, asynchronous tasks that must be run reliably. Most real-world apps need to perform long-running background tasks. To schedule a background task in an optimized and efficient way, use WorkManager.

I would like to thank Google who helped me understand this topic.

References

This article was created with support from these sites.

https://developer.android.com/codelabs/kotlin-android-training-work-manager#0

https://developer.android.com/reference/androidx/work/Constraints

https://developer.android.com/topic/libraries/architecture/workmanager

--

--