WorkManager Guide | Periodic and One-time | Android

Hamza Oban
Orion Innovation techClub
4 min readJul 31, 2023

--

Contents

  • What is WorkManager?
  • When is WorkManager used?
  • Implementing the WorkManager
  • How to define Periodic and One-time request?
  • What is a work chaining?
  • Observing the works.

What is WorkManager?

WorkManager is one of the Android Jetpack components and is used to manage the scheduling of jobs. It allows you to manage your work when your app is running in the background.

Types of persistent work

The following table summarizes the various types of work.

When is WorkManager used?

  • If you want to manage your work when your app is running in the background
  • If you want it to work compatible with different Android versions
  • If you want to manage your business according to different priority levels
  • If you want to trigger your jobs at a certain time or periodically

Implementing the WorkManager

To use WorkManager, you must first create a project in Android Studio. Next, the WorkManager library needs to be added to your app build.gradle file.

It doesn’t matter if you use Kotlin or Java.

dependencies {
def work_version = "2.5.0"

// Java
implementation "androidx.work:work-runtime:$work_version"

// Kotlin + coroutines
implementation "androidx.work:work-runtime-ktx:$work_version"
}

That is all. Now we can use WorkManager.

We will make an application that updates an Int value we created with SharedPreferences using WorkManager.

We include the Worker abstract class and implement the “doWork” function.

We create the SharedPreferences object and create an Int value and increase the value we created (with the value passed to us from MainActivity)

We can get the value sent here from MainActivity with input Data.

Input Data structure works with key-values pairs. Just like sharedPreferences or intent.

We call the refreshDatabase function we created in doWork.

class RefreshDatabase(val context: Context, workerParams: WorkerParameters) : Worker(context,
workerParams
) {

override fun doWork(): Result {
val data = inputData
val myNumber = data.getInt("intKey",0)
refreshDatabase(myNumber)
return Result.success()
}

private fun refreshDatabase(myNumber : Int){
val sharedPreferences = context.getSharedPreferences("com.example.workmanager",Context.MODE_PRIVATE)
var mySavedNumber = sharedPreferences.getInt("myNumber",0)
mySavedNumber += myNumber
println(mySavedNumber)
sharedPreferences.edit().putInt("myNumber",mySavedNumber).apply()

}
}

If you don’t know SharedPreferences, you can read my article.

Let’s see what we did in MainActivity.

We are sending the value to RefreshDatabase using the Data Class.

Work constraints

Declaratively define the optimal conditions for your work to run using work constraints. For example, run only when the device is on an unmetered network, when the device is idle, or when it has sufficient battery.

WorkRequest

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.

Let’s see how we do it when we want to do a one-time job.

One-time.

While creating the WorkRequest, it asks us for a type and we created the RefreshDatabase.

We used WorkManager to initialize WorkRequest.

Let’s see how we do it when we want to do a periodic job.

Unlike one-time, it asks us for two parameters. We say that we will update our database every 15 minutes in line with the parameters we have given.

Work chaining

For complex related work, chain individual work tasks together using an intuitive interface that allows you to control which pieces run sequentially and which run in parallel.

We specify which job to start with with the “beginWith” function. We determine the next jobs with the “then” function.

If you want to finish all work, you should call cancelAllWork function.

cancelAllWork

Observing the works

Finally, you can observe all jobs according to their id or tags, with LiveData support.

In our example, you can observe the status of jobs with WorkInfo.

Let’s see our example.

Conclusion

The WorkManager API is the recommended replacement for all previous Android background scheduling APIs, including FirebaseJobDispatcher, GcmNetworkManager, and Job Scheduler.

I hope it was a useful article for you! See you in my other articles.

Don’t forget to clap 👏

--

--