Using WorkManager — Android
WorkManager is intended for work that is not required to run immediately and required to run reliably even if the app exits or the device restarts. WorkManager is the recommended way for long-running background tasks
- Sending logs or analytics to backend services
- Periodically syncing application data with a server
Before Workmanger there were different job scheduling APIs, some of them worked on specific android versions or required google play services installed.
WorkManager on the other hand work on all android versions above ice cream sandwich and is independent of google play services
Under the hood WorkManager uses an underlying job dispatching service based on the following criteria:
Advantages of WorkManager
- One advantage of WorkManager from the previous APIs is that you can define constraints such that tasks are only executed if these constraints are met. For eg you want to download a user's favorite music only when the device is connected to Wifi.
- WorkManager also allows you to chain your request such that they run sequentially and one request will only execute if one before it is executed successfully
- WorkManager allows you to schedule work to run one- time or repeatedly using flexible scheduling windows. Work can be tagged and named as well, allowing you to schedule unique, replaceable work and monitor or cancel groups of work together. In addition, WorkManager adheres to power-saving features and best practices like Doze mode, so you don’t have to worry about it.
- Sometimes work fails. WorkManager offers flexible retry policies, including a configurable exponential backoff policy.
For WorkManager we need a work-runtime-ktx dependency in our project Gradle
Implementing WorkManager Requires 2 Steps
Creating a Worker Class where you define the work that you want to perform in the background.
The method dowork() runs asynchronously i.e it won’t pause our main UI thread while the task is being executed.
dowork() should return a result to show that work completed successfully. We can also include an output as part of the result.
In our use case as shown above we are fetching UserList. If all users are fetched we will return Result.success(). If there are any exceptions then we will return Result.retry() which will tell WorkManger to try it again later according to the retry policy
WorkManager allows two ways to schedule a work
If you want to schedule your work and it should execute just once then OneTimeWorkRequest is used
If you want that your work is repeated periodically, you can use a PeriodicWorkRequest
In our example below, we would be using PeriodicWorkRequest since we need to sync data after every few minutes regularly.
We have set some constraints which just say that for WorkManger to start network should be connected and the battery should not be low.
In PeriodicWorkRequest.Builder we need to define the repeatInterval which means that after how many intervals should the task be executed again. Over here we are using PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS which means 15 mins. So our task will run after every 15 mins.
That’s it for now!!
Thanks for reading and don’t forget to share with your fellow developers :)