Introduction to Work Manager (Android)

Asim LatifAsim Latif
3 min read

Contents

  • What is WorkManager?

  • Why and When to use WorkManager?

  • Persistent Work and Types

  • Features

  • Relation to Coroutines, Alarm Manager and other APIs

What is WorkManager?

WorkManager is an Android Jetpack library that simplifies and manages background tasks, ensuring efficient execution while adhering to system constraints. With its intuitive API, developers can schedule various types of tasks, such as one-time, periodic, and unique requests, seamlessly integrating background processing into their apps.

Whether it's uploading images, sending notifications, or performing data synchronization, WorkManager provides a robust solution for handling background work, enhancing app performance, and delivering a smoother user experience.

Say goodbye to manual management of background tasks and embrace the power and flexibility of WorkManager in your Android development workflow.

Why and When to use WorkManager?

Work Manager is an ideal solution for managing persistent tasks, which remain scheduled even in the event of app or system restarts. These tasks often involve background processing, making Work Manager a preferred choice for ensuring their seamless execution across various conditions.

WorkManager isn't designed for handling in-process background tasks that can be safely terminated if the app process ends. It also isn't a universal solution for all tasks requiring immediate execution.

Persistent Work

WorkManager handles three types of persistent work:

  • Immediate: Tasks that must begin immediately and complete soon. May be expedited.

  • Long Running: Tasks which might run for longer, potentially longer than 10 minutes.

  • Deferrable: Scheduled tasks that start at a later time and can run periodically.

Persistent work may be immediate, long running, or deferrable

TypePeriodicityExamples
ImmediateOne TimeFile Uploading, Encrypting Data
Long RunningOne Time or PeriodicImage Processing, Data Synchronization
DeferrableOne Time or PeriodicData Backup, Data Cleanup, Scheduled Notifications

Features

  • Work Constraints: Define the ideal circumstances for your work to execute by specifying work constraints declaratively. This may include conditions such as running only when the device is connected to an unmetered network, during idle periods, or when sufficient battery is available. Read more.

  • Scheduling: WorkManager empowers you to schedule tasks for one-time or repeated execution within flexible scheduling windows. With the ability to tag and name tasks, you can easily manage unique or replaceable work and monitor or cancel groups of tasks efficiently. It handles task persistence across device reboots through its internal SQLite database and seamlessly aligns with power-saving functionalities like Doze mode, ensuring worry-free background task management.

  • Retry Policy: WorkManager provides adaptable retry policies, allowing you to define actions to take in case of failure or cancellation. You can find out more about policies there.

  • Chaining Work: WorkManager enables you to link various tasks together, running them sequentially or simultaneously. This approach proves efficient for tasks reliant on the outputs of preceding ones, facilitating streamlined task execution.

  • Threading Interoperability: WorkManager seamlessly integrates with Coroutines and RxJava for built-in threading compatibility. Additionally, it offers the flexibility to incorporate custom asynchronous APIs as needed.

Relation with Other APIs

Although coroutines are suitable for specific use cases, they aren't intended for managing persistent tasks. It's essential to distinguish between coroutines, a concurrency framework, and WorkManager, a library specifically designed for persistent work.

Similarly, AlarmManager should only be utilized for tasks related to clocks or calendars, rather than for general background work.

The WorkManager API is the recommended replacement for all previous Android background scheduling APIs, including FirebaseJobDispatcher, GcmNetworkManager, and JobScheduler.

Useful Resources

0
Subscribe to my newsletter

Read articles from Asim Latif directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Asim Latif
Asim Latif