Working with WorkManager in Android
--
For persistent and long running tasks.
Why
There is always an urge in me to write something which can help others in a way which can lead to something unforgettable and cherishing forever. And here I am continuing writing but this time with a bigger vision and purpose. As you might know I am an Android developer and always try to learn anything new comes in Android ecosystem. This time it’s about WorkManager API or Android’s recommended solution for long and persistent background jobs. Few days back I am learning WorkManager and thought why not write an article which can help others as well as myself in future for reference.
Let’s begin with a short introduction.
Introduction
- WorkManager is a recommended solution in Android for long-running and persistent background tasks and jobs.
- By persistent we mean it remains scheduled even if your app restarts or your phone reboots.
- It is a part of Jetpack Architecture Components.
- It can be of two types: One time or periodic.
Let’s get started and follow few steps to implement WorkManager in your project. It will be a really basic steps and configurations for better understanding at first place.
Step 1: Add WorkManager Library in your app level build.gradle file with Kotlin & Coroutines support
Step 2: Add your worker class in the project
- Create a class which extends CoroutineWorker with required parameters like context and workerParameters.
- It will force you to override its doWork() method where you can implement your jobs or tasks that you want to run in background.
- You can return success or failure at the end of the doWork() method to convey the worker response.
Step 3: Add your worker request in your Activity or Fragment class
- Create a work request using either OneTimeWorkRequestBuilder or PeriodicWorkRequestBuilder.
- You can set initial delay in seconds, minutes, or hours before actual work starts.
- You can also pass input data to be used in the worker class.
- You can also set various constraints in the build method like network required or battery not low.
- You can also add any relevant tag as string to observe its status or output data.
- In case of Periodic Work Request, you can set minimum interval time and flex interval time between repeating work.
Step 4: Now enqueue your created work request in the WorkManager instance
- Create a work manager instance with activity or app context.
- Now enqueue you work request in your work manager instance.
- You can also chain related individual work requests in the work manager instance.
- You can also set backoff and retry policy in the instance in case of failure of worker implementation.
Step 5: Observe status of your workers or their data if any
- You can get the status of any worker class by its tag you added in the initialization time.
- You will get the current status of the worker class and any output data it is providing.
- You can perform any UI related operation by observing the status as it emits LiveData.
Final Source Code:
We have a a very basic worker class which is taking 2 inputs from our activity class in the form of integers and just adding them in its doWork() method and sending back the result in the output as output data.
- MyWorker.kt
- MainActivity.kt
Conclusion:
There are lots of various operations or customizations you can perform on WorkManager or using its APIs that can make your app implementing a reliable background job that can outlive app restarts or even phone reboots. If you can go through the documentation slowly with focus and determination then you can really easily understand how to use this beautiful and helpful API.
Hope that this article helps you in one or the other way. Do share and try to help others as well in your community if you believe in Sharing is Caring.