How to Use Android WorkManager

Sezer BOZKIR
Huawei Developers
Published in
3 min readJun 2, 2020

Hi everyone,

For a while I had a simple idea of timing in mind. Undoubtedly, the days that we all care and look forward to have been. You need a system that you want to write an application for jobs like this, that needs to work constantly in the back and pay attention to time. At this point, WorkManager comes into play and reaches our rescue …

What is WorkManager?

To make a full definition, it is the structure used for operations that will run as a result of timing or meeting certain conditions or that should be run at certain intervals.

Workmanager also has a nice feature that your application can continue its scheduled tasks when the device is restarted.

Application Integration

We add the code block to the build.gradle file of your app (as of now I wrote the current version in the documentation, please check this document when you integrate it in the future):

dependencies {
def work_version = "2.3.4"
// (Java only)
implementation "androidx.work:work-runtime:$work_version"
// Kotlin + coroutines
implementation "androidx.work:work-runtime-ktx:$work_version"
// optional - RxJava2 support
implementation "androidx.work:work-rxjava2:$work_version"
// optional - GCMNetworkManager support
implementation "androidx.work:work-gcm:$work_version"
// optional - Test helpers
androidTestImplementation "androidx.work:work-testing:$work_version"
}

Optional ones from the above codes may not be added depending on the content of your application.

Now let’s test it by creating a sample job:

public class UploadWorker extends Worker {    public UploadWorker(
@NonNull Context context,
@NonNull WorkerParameters params) {
super(context, params);
}
@Override
public Result doWork() {
// Do the work here--in this case, upload the images.
uploadImages() // Indicate whether the task finished successfully with the Result
return Result.success()
}
}

If the piece of code you are going to run is run only once, you can create it this way:

OneTimeWorkRequest uploadWorkRequest = new OneTimeWorkRequest.Builder(UploadWorker.class)
.build()

Or if your application needs to run periodically more than once, you can create it this way:

PeriodicWorkRequest uploadWorkRequest = new PeriodicWorkRequest.Builder(UploadWorker.class)
.build()

In workpieces that you periodically create, period intervals are 15 minutes by default. For this reason, there are 2 options on the Workmanager. (you can browse for details)

To include the workpiece in the process queue on the system:

WorkManager.getInstance(myContext).enqueue(uploadWorkRequest);

When you want to stop your job(you can browse for details):

WorkManager.cancelWorkById(uploadWorkRequest.getId());

Using Advanced WorkManager

You can take a look at this article what you wonder about the advanced optional features such as the code part of your application that works only while the device is charging, low battery power status, works only when Wi-fi is active.

If your application is already using FirebaseJobDispatcher, you can see how you can convert it by following these documents to migrate to WorkManager.

Or, if your application is already using GCMNetworkManager, you can browse this document to migrate to WorkManager.

If you are curious, you can ask under this title. See you in the next post 🙂

References:

https://medium.com/@aleesha/the-amazing-workmanager-in-android-ba046b69295
https://www.mobiler.dev/post/android-de-workmanager-a-gecis
https://developer.android.com/topic/libraries/architecture/workmanager/basics

--

--