Tagged Work

We can also associate one or more tags with our work requests. We can later get information about our outstanding work based on tags, or cancel work based on tags.

Tags are meant to be used as categories, to identify similar pieces of work that we might want to operate on in unison:

To add tags, just call addTag() one or more times on the request Builder:

    Constraints constraints = new Constraints.Builder()
      .setRequiredNetworkType(NetworkType.CONNECTED)
      .setRequiresBatteryNotLow(true)
      .build();
    OneTimeWorkRequest downloadWork =
      new OneTimeWorkRequest.Builder(DownloadWorker.class)
        .setConstraints(constraints)
        .setInputData(new Data.Builder()
          .putString(DownloadWorker.KEY_URL,
            "https://commonsware.com/Android/Android-1_0-CC.pdf")
          .putString(DownloadWorker.KEY_FILENAME, "oldbook.pdf")
          .build())
        .addTag("download")
        .build();

    WorkManager.getInstance(getApplication()).enqueue(downloadWork);
    val constraints = Constraints.Builder()
      .setRequiredNetworkType(NetworkType.CONNECTED)
      .setRequiresBatteryNotLow(true)
      .build()
    val downloadWork = OneTimeWorkRequest.Builder(DownloadWorker::class.java)
      .setConstraints(constraints)
      .setInputData(
        Data.Builder()
          .putString(
            DownloadWorker.KEY_URL,
            "https://commonsware.com/Android/Android-1_0-CC.pdf"
          )
          .putString(DownloadWorker.KEY_FILENAME, "oldbook.pdf")
          .build()
      )
      .addTag("download")
      .build()

    WorkManager.getInstance(getApplication()).enqueue(downloadWork)

Here, we use addTag() to tag this work as download.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.