Schedule tasks with WorkManager

Tram Ho

As you know Android Jetpack has brought us a lot of great libraries and Work Manager is one of them. So in this article, I will introduce to you this great library.

1. What is WorkManager?

  • WorkManager is a library that helps us manage jobs running in the background. It ensures that jobs are executed only when the job’s constraints are satisfied (for example, only when the battery is above 70%, or when the network is on the network, etc.). . With WorkManager, tasks or tasks are guaranteed to be performed even when we exit the application. – The current version of WorkManager is: 2.3.4

2. Why use WorkManager

2.1 Easy scheduling

WorkManager makes it easy to create asynchronous tasks and also allows you to explicitly specify when they are executed.

2.2 Easy query

  • We can easily query the status of tasks via the UUID
  • WorkManager not only provides the current status of the task but also allows the task to return specific data as key-value .

2.3 Support multiple android versions

Support android from api version 14 and above

2.4 Easily cancel scheduled tasks

Work Manager provides a UUID for each of your tasks, using this id allows you to cancel tasks at any time.

Let’s compare it with other tools.

3. When to use WorkManager?

  • When you want your work to be protected and definitely done => use Work Manager
  • When you want to add system constraints for your work such as only work when the battery is> 70% or only do the job when network => use WorkManager. You can refer to the constraints that WorkManager supports here

4. Work with Work Manager

4.1 Definition of Work Request

To create a WorkRequest, you simply need to execute the code below

However, you can set up additional constraints or manage your jobs easily

4.1.1 Add constraints to Work requests

  • You can add constraints to your work to let it know when it can run.
  • A job can have many constraints and a job is only executed when all its constraints are satisfied
  • Currently Work Manager supports constraints on: NetWork (Connected, Unmetered), Battery (Not low, Charing), Storage (Not low), Content (Max delay, Max update delay) Let’s see the example below to know How to add constraints to Work Request:

4.1.2 Initial Delays

In case your job has no constraints or all constraints are met when your job is processed, the system may choose to run the task immediately. If you do not want the task to run immediately, you can specify that your job will start after a specified delay.

4.1.3 Retries and Backoff Policy

If you ask WorkManager to retry the job, you can return result.retry () from your job.

Your work is then rescheduled with the default backoff policy and delay. The backoff delay specifies the minimum amount of time that must wait before a job is tested. The time to retry the job will increase to a default value

Below is an example of custom policy and backoff latency.

4.1.3 Define input / output for tasks

Your task may require that data be passed as an input parameter or returned to the result. For example, the image upload processing requires the URI of the uploaded image as input and may request the URL of the uploaded image as output.

Input and output values ​​are stored as key-value pairs in a Data object. The code below shows how you can put input data in WorkRequest.

The input and output data must be small in size and values ​​can be String, primitive types or their array variants. Maximum size is 10KB for Data objects

4.2 Listen to the status of the job

After calling enqueue () , Work Manager allows you to listen to the status of the job via the UUID or job tag.

Statuses provided by Work Manager

  • BLOCKED: The job is not completed
  • ENQUEUED: When constraints and time are met
  • RUNNING: When the job is executed
  • SUCCEEDED: When woker returns Result.success ()
  • CANCELLED: When the job is canceled
  • FAILED: When the worker returns Result.failure ()

4.3 Execute a job flow

WorkManager allows you to create and list a series of specified jobs with multiple tasks and determine their order. This is especially useful when you need to run certain tasks in a particular order.

  • beginWith () : defines the task or job sequence to be executed first
  • then () : determine the job to be executed next. then () only runs when the previous job is completed

  • Input Mergers : Allows to merge data of previous jobs and transfer them to later jobs. As in the above example, the output data of filter1, filter2 and filter3 jobs will be used as input for the compress job. WordManager provides two types of input merge: OverwritingInputMerger and ArrayCreatingInputMerger

4.4 Periodic work

  • WorkManager allows creating and executing routine jobs (repetitive tasks). Periodic work will be performed again after a certain time (minimum 15 minutes).
  • Routine jobs are not allowed to chain and may contain constraints. Time is calculated only when all constraints are met

Refer

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

Share the news now

Source : Viblo