Integrating Work Manager into Android

Tram Ho

Running background tasks is a very difficult task in Android. Working with AlarmManager, Firebase JobDispatcher or Job Dispatcher does not guarantee execution of tasks in Android. Is this a big deal?

WorkManager has solved this problem for us.

What is WorkManager?

WorkManager is part of the Android Jetpack library, which makes it easy to schedule asynchronous, asynchronous tasks that are expected to run even when the application exits or restarts the device, i.e. even Your application restarts due to any Job Manager task that ensures a scheduled task will execute again. Is that cool?

In this blog, we'll talk about how WorkManager is integrated into your project and much more advanced features and customization make it easy for your life to schedule tasks.

So let's get started.

To integrate job manager in your project,

Now, as a next step, we will create a Worker class. The Worker class is responsible for performing work synchronously on a background thread provided by the job manager.

In the upper class,

  • The doWork () method is responsible for executing your task in the background thread. Any task you want to perform must be written here.
  • The result returns the status of the job performed in the doWork () method. If it returns result.success (), the action is successful if the status is result.failure (), the task fails and finally, if it returns result.retry (), it means The task will reappear after some time.

Now, let's configure how to perform the task

We need to create a WorkRequest to determine how and when the job should be run. It has two types,

  • OneTimeWorkRequest – Run the task only once
  • PeriodicWorkTimeRequest – Run the task after a certain amount of time.

and once we have determined our work requirements, we can schedule the task using

That's it, this is how you can schedule your task using Work Manager. Is this simple?

Now, let's talk about the customizations we can do during the task execution process.

We can add specific constraints in WorkRequest to customize it. To add constraints we use

And to add it in our work request we use

Here, we set the above defined constraints to the previously defined workRequest. Now, this job request will only run if all the constraints are met.

We may also set Recurring Quests to run after a certain amount of time. To run a recurring workRequest, we use,

This will run every 1 hour periodically as we have set the interval to 1 hour.

The minimum time to run a recurring task is 15 minutes.

If you do not want the task to run immediately, you can specify your work to start after a minimum initial delay by using:

This will run after an initial delay of 10 minutes.

Now let's check out some of the great features we have in Job Manager

Check the status of your WorkManager

If we want to perform some task when WorkManager successfully executes a task such as displaying a Toast or something else, we need to check the status of the task. To check the status we use,

Here, we observe the status of LiveData to update the user interface

Stringing of tasks

We can chain multiple tasks in two ways.

  • Chaining in Series
  • Parallel Chaining

Talk about them one by one.

Chaining in Series

Suppose we have two workRequest,

and we need to string them into strings. To do this, we will use

Here, first, the implementation will start with your WorkRequest WorkRequest and then it will execute the second one. This is called a sequence of tasks.

Parallel Chaining

In this case, we can chain parallel tasks using the following

Here, work1, work2 and work3 will execute in parallel and then when all of them are executed, only work4 will execute and work5 sequentially.

Note: If the first task fails, the next task executes a failure response and if the first task is canceled, all of the following tasks will also be canceled.

To cancel the task we use,

Input / output for your task

While running a job / task, we may need some input for the employee to run. For example, fetching the current user details will require the user id of the user. To overcome that as the input we use,

and to get input into the worker class we have,

Here, we get the input using getInputData (). GetString () and we pass it to getDetail () as a parameter to get the user details.

Now, to convert the User's name as an output, we pass the output in Result.Success (/ ** Your Output ** /).

This is how we can integrate the job manager in our Android project.

Wish you happy learning

Link reference

Share the news now

Source : Viblo