WorkManager – Scheduling Tasks Using WorkManager

WorkManager is an Android Jetpack library that runs deferrable and asynchronous tasks when the work’s constraints are satisfied.

Workmanager is usually recommended when running background work tasks.
Work’s constraints are requirements that need to be meet before executing certain task.
WorkManager can be used to schedule asynchronous task to happen one-time or periodic.

One-time task will only happen once while periodic task happen periodically after a certain number of time.

Advantages of using WorkManager to schedule task include.

  • Backward compatible with API 23+.
  • Scheduling of asynchronous tasks.
  • Adhere to execution even if app or device restarts.
  • Chain task together.
  • WorkManager constraints are set conditions must be met for task to take place i.e like (network availability).

To understand it better we are going to develop an application which sends notification to user at a specified time.

We are going to start with one-time where notification will be sent only once, then change to periodic where notification will be sent periodically after specific number of time.

Our expected output

WorkManager

Adding WorkManager dependency in our gradle file

Creating our background work which will be executed by the WorkManager

Create a new class and name it NotificationHandler, this call will extend out worker class and then overide the dowork().

We are going to define our work in this class, The work will be sending notification to user reminding him of new event at a specific time.

Let’s create our notification and the pass it to do work();

The return result in work() can return the following:
Result.Success() to indicate work was successfully executed.
Result.Failure() to indicate work failed to execute.
Result.Retry() to indicate work should be retried at a later time.

Let’s now create our work request

WorkRequest defines when and how the task should be executed, A task can be one-off or periodic.
For one-off WorkRequest task we use OneTimeWorkRequest and for PeriodicWorkRequest we use PeriodicWorkRequest.
We are going to start with one-off and later perform the same work using PeriodicWorkRequest.
Let’s create one-off WorkRequest method inside our NotificationHandler.class

setInitialDelay specifies delay time before the task is executed .
setConstraints tells WorkManager to execute the task if certain conditions are met.
In our case we only have one constraints which mean for our work to be executed the device should be charging.

Finally let’s now perform our work using WorkManager

Inside you MainActivity oneOffRequest() in the NotificationHandler class;

Adding periodic WorkRequest

Create a new method inside NotificationHandler class

PeriodicWorkRequest we pass three parameters

  • Our context.
  • Interval at which the task will be executed
  • TimeUnit for Interval;

In your MainActivity Call the periodicWorkRequest() instead of oneOffRequest();

Congration you have finished creating sending notifications using WorkManager for both one-off and periodic.
Use the link below to get source code:
Android Sending Notification Using WorkManager
Kindly comment below for further help or guidance.

WorkManager

Learn more about RoomDatabase. Use the link below. https://larntech.net/android-roomdatabase-livedata-and-viewmodel/