Android WorkManager Tutorial – AndroidWave

Android WorkManager Tutorial

Welcome back our

tutorials series. In this article, we will learn what is WorkManager API in Android ? how it works and what are its advantages.

So let’s get started.

Since Marshmallow, The Android dev team is continuously working on battery optimizations. After that team introduced Doze mode.

Then in Oreo imposed various kind of Before WorkManager, we use various job scheduler for performing background task, such as Firebase JobDispatcher, Job Scheduler and Alarm Manager + Broadcast receivers. So for the developer perspective, it is difficult to choose which scheduler should use and which one is good. So the Work Manager handles these kinds of stuff. We have to pass the task to the WorkManager and It uses all this Firebase Job Dispatcher, Alarm Manager + Broadcast

Receivers, Job Scheduler to perform the background task depending on the requirement.

WorkManager is basically a task scheduler,

It makes it easy to specify the asynchronous task and when they should run. The Work Manager API helps create the task and hand it to the Work Manager to run immediately or at an appropriate time as mentioned. For example, you might point your app to download new resources from the network from time to time and now the downloading is a task and you can set up this task to run at an appropriate time depending on the availability of the WIFI network or when the device is charging.

So this way you can schedule a task using WorkManager.

Advantages of WorkManager

How it works

Before moving forward, We have to understand the class and concept of WorkManager. Let’s understand what are various base classes that are used for Job Scheduling.

– Worker

It specifies what task to perform, The WorkManager API include an abstract worker class and You need to extends this class and perform the work.

– WorkRequest

WorkRequest represents an individual task that is to be performed. Now this WorkRequest, you can add values details for the work.

Such as constraint or you can also add data while creating the request

WorkRequest can be of to type

– WorkManager

The WorkManager class in enqueues and manages all the work request. We pass work request object to this WorkManager to

enqueue the task.

– WorkInfo

WorkInfo contains the information about a particular task, The work manager provides

for each of the work request objects, We can observe this and get the current status of the task.

Step for implementation WorkManager to Schedule Tasks

Create a new project and add WorkManager dependency in app/. fileCreate a base class of WorkerCreate WorkRequest

Enqueue the request with WorkManager.Fetch the particular task statusLets we will create

application using the above steps.

1. Add dependency

For using WorkManager we have to add dependency in app/build.gradle file. So let’s open the app build.gradle file and add below lines.

implementation “android.arch.work:work-runtime:1.0.0”

In this demo

we will create a layout. This layout will contain TextView and Button. After that, we will set onClickListener() and this event we will enqueue the WorkRequest to WorkManager and shows the status on TextView. Let open the AndroidStudio and create a new project with basic activity template.

Add Button and TextView in activity_main.xml layout files.

Leave a Reply

Your email address will not be published. Required fields are marked *