In a previous iteration of our application, we implemented a periodic task using WorkManager as follows:
WorkManager.getInstance(context).enqueue(
new PeriodicWorkRequest.Builder(OurClassName.class, 8, TimeUnit.HOURS)
.setConstraints(constraints)
.setInputData(OurClassName.getParameterBundle())
.build()
);
This code snippet schedules a job that executes every 8 hours.
However, we've encountered an issue where these tasks cannot be canceled since they were not assigned a unique 'name' or 'tag'.
Complicating matters, due to a bug, this scheduling code was executed every time the app launched, resulting in a significant accumulation of identical, scheduled jobs.
Despite attempting to use both WorkManager and JobScheduler's cancelAll methods as a broad-stroke solution, the jobs persist.
How can we effectively cancel these periodically scheduled tasks, especially considering they lack unique identifiers?
The
WorkRequestcreated by the.build()function can be used to fetch theidto manage its operation via WorkManager instance. Example:This is expected due to the use of
enqueue. UsingenqueueUniquePeriodicWorkwould work as expected.For cancellations, see: https://developer.android.com/develop/background-work/background-tasks/persistent/how-to/manage-work#cancelling
Assuming you need to cancel all the previous
WorkRequests (say on a new app update), you can use -WorkManager.getInstance(context).cancelAllWork()orWorkManager.getInstance(context).cancelAllWorkById(workId)if you have the IDs & then register the new WorkRequests viaenqueueUniquePeriodicWork.Disclaimer from the docs: