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 like pieces of work that we might want to operate on in unison:
- All downloads
- All work associated with some particular database table
- All work associated with some account
- And so on
To add tags, just call addTag()
one or more times on the request Builder
:
public void doTheDownload() {
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().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.