Background work in Android : WorkManager .

marouene khadhraoui
3 min readApr 25, 2021

Lets Imagine that we’re developing an app and we want to upload a large file or multiple files to a server . The operation can take up to many minutes , the user may want to quit the app and do other work on his device but upload must continue , we have to make sure to upload only if we have internet connection , we also need to be aware of the battery life of the device .

How to develop this upload function while respecting all these constraint ? Well happily our friends at Android have the solution : WorkManager .

What is WorkManager ?

WorkManager is an API that is a part of the Jetpack Library . It permits us of performing asynchronous operations, even when the app is closed or the device restarted . It can work back to API 14 so its compatible with the majority of working Android devices .

The current stable version is 2.5.0 , but the 2.6.0 alpha version offers great features such as using App Startup for initialization , and Couroutines .

Important Note :

WorkManager does not work by default in some devices by manufacturers like Xiamoi , Huawei , Oppo etc … The reason being that these manufacturers have their own battery saving process and it is not controlled by the Android system . A workaround for this is to disable the app battery saver for the application . Not a production ready solution , but the only one so far .

Implementation :

First of all , we need to add the dependency in our app’s Gradle file :

Now we move to declaring our worker .

OurFirstWorker extends the Worker class . We override the doWork() method and we must return a result of type Result . Result.success() means that the worker has done the task successfully , Result.failure() will stop the worker in the case of a failure . Now that we declared our worker we need to call it in our activity or fragment of choice .

We submitted our worker to the system and added a constraint with the setConstraints() method . The worker will only launch if we have internet connection , other constraints can be added :

We also might need to pass data to our worker :

Asynchronous work :

Let us roll back for a moment and look to the example mentioned in the introduction and use our worker to upload a large file to a Firebase server .

This is an asynchronous operation that can take up to multiple minutes , and needs to be done without blocking the UI and preventing the user from interacting with the app . The standard WorkManager is only capable of doing synchronous work , but there is no need to worry , ListenableWorker comes to the rescue .

ListenableWorker comes with the advantage of letting us handle the threading . It only signals the start and stop of the work and gives us the freedom to handle the rest . Now lets get to its implementation wich is different then the standard WorkManager .

We will implement the example of uploading a video file to Firebase .

Now , thats a lot of code . Lets try to understand it bit by bit .

  • First of all , we substituted the doWork() function for startWork() .
  • Our worker class is now extending ListenableWorker .
  • startWork() returns a ListenableFuture result , an interface that we will set with the result . It uses Concurrence wich moves the work of the main thread so we dont have to do it manually and takes advantage of one of the most exciting concepts in Kotlin : couroutines . Do not forget to add “androidx.concurrent:concurrent-futures-ktx:1.1.0” in your app gradle file .
  • setForegroundAsync(createForegroundInfo(progress)) : the user has to always be aware if some background work is done , specially if ts a long running task so sso we most inform him by showing a notification that will display when the WorkManager is doing its work and will be dismissed once its stopped . Therefore ,starting from the 2.3.0 release, createForegroundInfo() is responsible of showing the notification , and calls createChannel() , that creates a channel for our notification ( Since API 26 , a channel must be created for each notification ) .

Thats it ! You can now use WorkManager and customize it to your need .

Thank you for reading , Share the article and leave me a comment . Every interaction is deeply appreciated .

--

--