talk to us

Talk to us

Talk to us

Call us at :+91 960 622 2779

Or, let us contact you

×

Please enter the name

Please enter the company

Please enter the email

Please enter the phone

Please enter the location

Please select an option

Please enter the details

Please verify captcha

Top
August 16, 2019

What is WorkerManager in Android?

Blogs author avatar

Posted by:
Pankaj Pathak

WorkerManager is a beautiful concept in android for scheduling and managing background tasks at a specific time or immediately. WorkerManager uses libraries like AlarmManager, Firebase Job Dispatcher, JobScheduler, and other libraries for performing tasks.

The main advantage of WorkManager API is that we don't need to worry about system compatibility, because it chooses the appropriate way to run a task itself, depending on factors such as API level and the application state. With WorkerManager we can define execution sequences of various tasks, so that they can run one after another, and we can cancel a task at any stage.

what-is-worker-manager-in-android-inner-1

Classes and Concepts:

WorkerManager APIs consist of different classes and abstract classes for scheduling task.

1. Worker: Worker is an abstract class which defines a task or job that has to be performed in the background. Worker class contains just the information about how to perform the task but it doesn't have information about what task has to be performed.

2. WorkRequest: WorkerRequest is responsible for performing the task which has been specified in Worker. We need to create a WorkRequest for scheduling any work. WorkRequest contains the unique id of the task to be performed, which is used to identify each 'work' present in the queue. We can add constraints to a WorkRequest for explaining under which condition the task has to be performed.

3. WorkRequest is an abstract class, which has two direct subclasses OneTimeWorkRequest and PeriodicWorkRequest. OneTimeWorkRequest is used for scheduling the task which need to be executed only once, and PeriodicWorkRequest is used for performing a task multiple times, based on certain conditions.

what-is-worker-manager-in-android-inner-2

Steps to schedule Tasks Using WorkManger

what-is-worker-manager-in-android-inner-3

Step 1: Create one class extending Worker abstract class, and override doWork() method. We need to define the work that has to be done inside this method. This method returns WorkerResult which has three possible values:

WorkerResult.SUCCESS : It means that the work has been performed successfully.

WorkerResult.RETRY : Specifies that the work got interrupted due to some error and will retry to perform the task after some time.

WorkerResult.FAILURE : Indicates that the task has not been performed because of some error or issue.

public class TestWorker extends Worker {
@Override
public Worker.Result doWork() {
Log.i("Work:", "Define the work here to implement");
return Worker.Result.SUCCESS;
}
}

Step 2: Create a work request to schedule the task defined in doWork() method in step1. As already discussed, there are two ways of scheduling the work OneTimeWorkRequest and PeriodicWorkRequest.

OneTimeWorkRequest oneTimeWorkRequest =
new OneTimeWorkRequest.Builder(TestWorker.class).build();

If we need to perform some work periodically then we can go for PeriodicWorkRequest. We need to pass three parameters in the constructor Worker, WorkerTime after which the task will be repeated and time unit.

PeriodicWorkRequest periodicWorkRequest =
new PeriodicWorkRequest.Builder(TestWorker.class, 30, TimeUnit.MINUTES)
.build();

Step3: Get the WorkManager instance and add WorkRequest object in the queue by using enqueue() method:

WorkManager.getInstance().enqueue(oneTimeWorkRequest);

Constraints

Constraints are used to define some scenarios which are required to perform the task defined in Worker. If we do not define any constraints, then the work will be performed immediately.

We can define constraints based on our requirement such as Network state of device, Device is on Charging or not, Wifi connected, Device ideal etc.

Constraints constraints = new Constraints.Builder()
.setRequiresCharging(true)
.setRequiredNetworkType(NetworkType.CONNECTED)
.setRequiresBatteryNotLow(false)
.setRequiresDeviceIdle(true)
.build();
oneTimeWorkRequest.setConstraints(constraints);

what-is-worker-manager-in-android-inner-4

Chaining the work:

Chaining concept is very useful when we want to execute tasks sequentially, parallelly or as per our requirement. We just need to define the order of execution for each work. If any work defined in the sequence fails then the dependent works will also not be executed.

There are two methods defined for chaining the task beginWith() and then(). Both the methods expects WorkRequest object as argument.

beginWith(): This method is used to define the first WorkRequest which has to be executed at the starting.

then(): We can define any number of then calls after beginWith() method in the required order.

WorkManager.getInstance()
.beginWith(WorkRequest1)
.then(WorkRequest2)
.then(WorkRequest3)
.then(WorkRequest4)
.then(WorkRequest5)
.enqueue();

In the above example the work will execute in the following sequence:

WorkRequest1 ---> WorkRequest2 ---> WorkRequest3 ---> WorkRequest4 ---> WorkRequest5

Now let's suppose that we have a requirement for running WorkRequest1 and WorkRequest2 parallelly, after completion of which we need to execute WorkRequest3 and WorkRequest4 parallelly and after finishing of them need to process with WorkRequest5. This use case we can define in the following way:

WorkManager.getInstance()
.beginWith(WorkRequest1,WorkRequest2)
.then(WorkRequest3,WorkRequest4)
.then(WorkRequest5)
.enqueue();

Combining multiple chains:

WorkerManager provides WorkContinuation class which holds one chain of work and we can combine multiple chains of work by using combine() method defined in WorkContinuation.

Let's assume that we have two chains of work , chain1(WorkRequest1,WorkRequest2) and chain2(WorkRequest3,WorkRequest4) and one single work WorkRequest5, then we can perform this in the following way:

WorkContinuation workContinuation1 = WorkManager.getInstance()
.beginWith(WorkRequest1,WorkRequest2);

WorkContinuation workContinuation2 = WorkManager.getInstance()
.beginWith(WorkRequest3,WorkRequest4);

WorkContinuation workContinuation3 = WorkContinuation
.combine(workContinuation1, workContinuation2)
.then(workContinuation5);
workContinuation3.enqueue();

For any assistance, contact Appiness Interactive, an App Development Company in Bangalore. 

Next Blog Previous Blog All Blogs