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.
2. Create a base class of Worker
Create a base class of Worker class and override un-implemented methods and super constructor
package com.wave.workmanagerexample;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.NotificationCompat;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
/**
* Created on : Mar 26, 2019
* Author
: AndroidWave
*/
public class NotificationWorker extends Worker {
private static final String WORK_RESULT = “work_result”;
public NotificationWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
Data taskData = getInputData();
String taskDataString = taskData.getString(MainActivity.MESSAGE_STATUS);
showNotification(“WorkManager”, taskDataString != null ? taskDataString : “Message has been Sent”);
Data outputData = new Data.Builder().putString(WORK_RESULT, “Jobs Finished”).build();
return Result.success(outputData);
}
private void showNotification(String task, String desc) {
NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = “task_channel”;
String channelName = “task_name”;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new
NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), channelId)
.setContentTitle(task)
.setContentText(desc)
.setSmallIcon(R.mipmap.ic_launcher);
manager.notify(1, builder.build());
}
}
3. Create WorkRequest
Let’s move to MainActivity and create a WorkRequest to execute the work that we just created. Now first we will create WorkManager. This work manager will enqueue and manage our work request.
WorkManager mWorkManager = WorkManager.getInstance();
Now we will create OneTimeWorkRequest because I want to create a task that will be executed just once.
OneTimeWorkRequest mRequest = new OneTimeWorkRequest.Builder(NotificationWorker.class).build();
Using this code we built work request, that will be executed one time only
4. Enqueue the request with WorkManager
Now onClick() of the button we will enqueue this request using the WorkManager. So that’s all you need to do.
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mWorkManager.enqueue(mRequest);
}
});
5. Fetch the particular task status
Let us fetch some information about this particular task and display it on tvStatus TextView. We will do that using WorkInfo class. The work manager provides
for each of the work request objects, We can observe this and get the current status of the task.
mWorkManager.getWorkInfoByIdLiveData(mRequest.getId()).observe(this, new Observer
@Override
public void onChanged(@Nullable WorkInfo workInfo) {
if (workInfo != null) {
WorkInfo.State state = workInfo.getState();
tvStatus.append(state.toString() + “\n”);
}
}
});
Finally, MainActivity looks like this.
package com.wave.workmanagerexample;
import android.arch.lifecycle.Observer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;
public class MainActivity extends AppCompatActivity {
public static final String MESSAGE_STATUS = “message_status”;
TextView tvStatus;
Button btnSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvStatus = findViewById(R.id.tvStatus);
btnSend = findViewById(R.id.btnSend);
final WorkManager mWorkManager = WorkManager.getInstance();
final OneTimeWorkRequest mRequest = new OneTimeWorkRequest.Builder(NotificationWorker.class).build();
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mWorkManager.enqueue(mRequest);
}
});
mWorkManager.getWorkInfoByIdLiveData(mRequest.getId()).observe(this, new Observer
@Override
public void onChanged(@Nullable WorkInfo workInfo) {
if (workInfo != null) {
WorkInfo.State state = workInfo.getState();
tvStatus.append(state.toString() + “\n”);
}
}
});
}
}
Conclusion
Now run the application and see our app the up and running. Now click on Send Notification button. the Job status will show on TextView.
So this is a basic tutorial of WorkManager. I hope you get a clear idea about Manager. In the upcoming article, I will explain
How to run a task in a specific condition.
If it’s helpful for you, then help me by sharing this post with all your friends who learning android app development.
Iou have any queries, feel free to ask them in the comment section below.
Happy Coding 🙂
May 22, 2019
April 14, 2019
April 8, 2019