#9 Mastering Background Processing in Android with WorkManager: A Guide to Efficient Background Processing

Master Background Processing in Android with WorkManager: Guide to Efficient Background Processing using Android & Kotlin

#9 Mastering Background Processing in Android with WorkManager: A Guide to Efficient Background Processing

Introduction

In the world of mobile app development, performing background tasks efficiently is crucial for providing a smooth user experience. Android provides various APIs for background processing, but the recommended solution for persistent work is WorkManager. Part of Android Jetpack, WorkManager offers a simplified and consistent API for scheduling tasks that need to run in the background, even across app restarts and system reboots.

In this blog post, we will explore the power of WorkManager and learn how to schedule different types of tasks, define work constraints, handle work chaining, and integrate with other threading frameworks. We will also discuss the benefits of using WorkManager for reliable work and how it replaces deprecated APIs like FirebaseJobDispatcher, GcmNetworkManager, and Job Scheduler.

1. Types of Persistent Work

WorkManager handles three types of persistent work:

Immediate

Immediate tasks should begin execution immediately and complete soon. They can also be expedited in cases where priority is required.

Long Running

Long-running tasks are those that might run for a longer duration, potentially exceeding 10 minutes. These tasks can be scheduled to run one time or periodically.

Deferrable

Deferrable tasks are scheduled to start at a later time and can run periodically as well. They provide flexibility in executing tasks based on specific time intervals or conditions.

2. Features of WorkManager

WorkManager offers several key features that make it a powerful tool for background processing:

Work Constraints

Declaratively define optimal conditions for your work to run using work constraints. For example, you can specify that a task should only run when the device is on an unmetered network, when the device is idle, or when it has a sufficient battery.

Robust Scheduling

WorkManager allows you to schedule work to run one-time or repeatedly using flexible scheduling windows. You can tag and name your work to schedule unique, replaceable tasks and monitor or cancel groups of work together.

Expedited Work

You can use WorkManager to schedule immediate work for execution in the background. Expedited work is useful for tasks that are important to the user and complete within a few minutes.

Flexible Retry Policy

WorkManager offers flexible retry policies, including a configurable exponential backoff policy, to handle cases where work might fail.

Work Chaining

For complex related work, you can chain individual work tasks together using an intuitive interface that allows you to control which pieces run sequentially and which run in parallel. Output data from one task can be passed to the next automatically.

Built-In Threading Interoperability

WorkManager seamlessly integrates with Coroutines and RxJava, providing flexibility to plug in your own asynchronous APIs for better control over threading.

3. Scheduling Immediate Work

To schedule immediate work in WorkManager, you can use the OneTimeWorkRequest class along with a Worker implementation. You can also set the task as expedited if it requires higher-priority execution.

1val workRequest = OneTimeWorkRequestBuilder<MyWorker>()
2 .setExpedited(true) // Set as expedited if required
3 .build()
4
5WorkManager.getInstance(context).enqueue(workRequest)

4. Scheduling Long Running Work

For long-running tasks, you can use any WorkRequest subclass along with a corresponding Worker implementation. If you want to show a notification for the ongoing task, you can call setForeground() in the Worker class.

1val longRunningWork = PeriodicWorkRequestBuilder<MyWorker>(repeatInterval, repeatIntervalTimeUnit)
2 .setInputData(myData)
3 .setForeground(true) // Show notification for ongoing task
4 .build()
5
6WorkManager.getInstance(context).enqueue(longRunningWork)

5. Scheduling Deferrable Work

To schedule deferrable work that starts at a later time and can run periodically, you can use PeriodicWorkRequest along with a Worker implementation.

1val deferrableWork = PeriodicWorkRequestBuilder<MyWorker>(repeatInterval, repeatIntervalTimeUnit)
2 .setInputData(myData)
3 .build()
4
5WorkManager.getInstance(context).enqueue(deferrableWork)

6. Defining Work Constraints

Work constraints allow you to specify optimal conditions for your work to run. You can define constraints such as network connectivity, device charging status, battery level, and more.

1val workConstraints = Constraints.Builder()
2 .setRequiredNetworkType(NetworkType.UNMETERED)
3 .setRequiresCharging(true)
4 .setRequiresBatteryNotLow(true)
5 .build()
6
7val constrainedWork = OneTimeWorkRequestBuilder<MyWorker>()
8 .setConstraints(workConstraints)
9 .build()
10
11WorkManager.getInstance(context).enqueue(constrainedWork)

7. Handling Work Chaining

Work chaining allows you to chain multiple work tasks together, defining dependencies between them. The output data from one task can be passed as input data to the next task automatically.

1val cleanupWork = OneTimeWorkRequestBuilder<CleanupWorker>().build()
2val waterColorFilterWork = OneTimeWorkRequestBuilder<WaterColorFilterWorker>().build()
3val grayScaleFilterWork = OneTimeWorkRequestBuilder<GrayScaleFilterWorker>().build()
4val blurEffectFilterWork = OneTimeWorkRequestBuilder<BlurEffectFilterWorker>().build()
5
6val saveImageToGalleryWork = OneTimeWorkRequestBuilder<SaveImageToGalleryWorker>()
7 .addTag(Constants.TAG_OUTPUT)
8 .build()
9
10val uploadWork = OneTimeWorkRequestBuilder<UploadWorker>()
11 .addTag(Constants.TAG_OUTPUT)
12 .build()
13
14val continuation = WorkManager.getInstance(context)
15 .beginUniqueWork(Constants.IMAGE_MANIPULATION_WORK_NAME, ExistingWorkPolicy.REPLACE, cleanupWork)
16 .then(waterColorFilterWork)
17 .then(grayScaleFilterWork)
18 .then(blurEffectFilterWork)
19 .then(if (save) saveImageToGalleryWork else uploadWork)
20
21continuation.enqueue()

8. Built-In Threading Interoperability

One of the advantages of using WorkManager is its seamless integration with Coroutines and RxJava. You can easily combine these threading frameworks with WorkManager to handle asynchronous operations within your tasks.

For example, using Coroutines with WorkManager:

1class MyCoroutineWorker(appContext: Context, workerParams: WorkerParameters) :
2 CoroutineWorker(appContext, workerParams) {
3
4 override suspend fun doWork(): Result {
5 // Perform asynchronous operations using Coroutines
6 return Result.success()
7 }
8}

9. Using WorkManager for Reliable Work

WorkManager is designed for reliable work that needs to run even if the user navigates away from the app or if the device restarts. It is suitable for tasks like sending logs or analytics to backend services or periodically syncing application data with a server.

However, it is not intended for in-process background work that can be safely terminated if the app process goes away. It is also not a general solution for all work that requires immediate execution. In such cases, other solutions like coroutines or AlarmManager should be considered.

10. Relationship to Other APIs

While coroutines are recommended for certain use cases that don’t require persistence, they should not be used for persistent work. Coroutines are primarily a concurrency framework, whereas WorkManager is specifically designed for persistent background processing.

AlarmManager should only be used for alarms related to clocks or calendars and not for general background work. Unlike WorkManager, AlarmManager wakes up a device from Doze mode, which is less efficient in terms of power and resource management.

11. Getting Started with WorkManager

To start using WorkManager in your Android app, follow these steps:

  1. Add the necessary dependencies to your project’s build.gradle file.

  2. Define a Worker subclass and implement the required doWork() method.

  3. Create an instance of OneTimeWorkRequest or PeriodicWorkRequest using the builder pattern.

  4. Enqueue the work request using WorkManager.getInstance(context).enqueue().

For detailed instructions and code examples, refer to the official Android documentation on getting started with WorkManager.

12. Additional Resources

Here are some additional resources where you can find more information about WorkManager:

With its powerful features and seamless integration with other threading frameworks, WorkManager is undoubtedly the go-to solution for background processing in Android apps. By intelligently scheduling tasks and defining constraints, you can ensure that your app performs efficiently while providing a great user experience.

Remember to handle different types of persistent work appropriately, define work constraints based on optimal conditions, utilize work chaining for complex tasks, and leverage built-in threading interoperability with Coroutines and RxJava. Start using WorkManager today and take your Android background processing to the next level!

Comments