Android Jetpack - Work Manager

Easiest way to Schedule

Balakrishnan
Tech Log
4 min readJun 6, 2018

--

Google launched Android Jetpack, a set of components, tools, and guidance designed to accelerate app development at 2018 I/O developer conference.It is designed in such way that we use either all of them together or use one or more based on our needs. Since Architecture Component comes into play handling the life cycle becomes lot more simpler.

Android Jet Pack Library Overview

Which here we are going to discuss something which is essential for development. Most of the app that we are developing has a process that need to be done with in the time span or in background.

But expecting same set of code could do job for is greedy that android restricting the amount of work a app can do in background and app running time in background. Hence here comes the time savior Work Manager

Okay, lets see that available API for scheduling the tasks,

Simple comparison

Here is simple library that works on Work Manager

When you develop a app you need to give support for lower end devices too you have to write same logic for devices below API level 21 and above API level 21 twice. We also have AlarmManager, SyncAdapter, AbstractThreadedSyncAdapter to do task one or more time based on constraint(Time interval, Network State and excetera).

After working on multiple projects with the same functionalities(periodic reminders, download tasks, online triggers) for many times with Alarm manager, Job scheduler and Firebase Job-dispacher. I thought why not the entire tasks to be done with a single savior WorkManager. So here it is, my post talking about the WorkManager implementation and it’s usage.

Work Manager promises guaranteed execution of work irrespective of circumstances(It also works after the device reboot) and it has its own Database to maintain tasks. Moreover it’s easy to schedule, cancel and manage multiple works sequential and parallel order of execution.

Lets see the important classes of Work manager,

  • Worker - logical implementation to do task
  • Work Request - specifying things like the circumstances under which the task should run.
  • WorkStatus -status contains the information about the task such as id, tag, state, outputdata.
  • State - Current state of the request ENQUEUED, RUNNING, SUCCEEDED, FAILED, BLOCKED, CANCELLED.

Note: To import the WorkManager library into your Android project, see Adding Components to your Project.

Worker Class implementation

NotificationWorker.java class is Worker class responsible for doing work in background

Creating a simple request for Worker

We have two work request types

  • OneTimeWorkRequest -when task is required to run one time
  • PeriodicWorkRequest- when task is required to run periodically

OneTimeWorkRequest

OneTimeRequest without input

PeriodicWorkRequest

Note: Periodic work interval minimum time is 900000 seconds or in other words 15 minutes

PeriodicWorkRequest without input

Input and Output Data

So we can set data for Individual work (input data) and we need to define observer to read the output data. Some times it throws ‘IllegalStateException’ since Data is Serialized, Max size of data has limit of 10KB.

PeriodicWorkRequest with input

To get the input data in Worker class use the following in Worker class

To set the output data after task is completed do this is in Worker class

Bonus🏆

Chained Request

Chained Request is used when there are multiple task and they need to be executed in certain order

For chaining the tasks or ordering the task execution we use WorkCountinuation. It manages the order of execution and it executes the task sequentially.

Parallel Task execution

For execution of multiple tasks paralleled manner we use following method

Conclusion

If you run your app in debug mode you can see this in log cat, which provides information about Process allocation, scheduling information and using which services it is executed.

Logcat logs

In next post I will write about chaining multiple task efficiently by using the work policies, handling input methods and outputs, Parallel task execution and Input merger. Stay tuned for more

Get Sample Code here

If you have any questions or comments, please feel free to drop them in the comments section below or send me a tweet.

Click the 💚 below to show your support and share it with other fellow Medium users.

References

Medium post , Android developer

--

--