4 Ways To Cancel Android WorkManager Tasks
A short guide about all possible WorkManager cancellation methods
The Android WorkManager API is made for scheduling deferrable, asynchronous tasks. We reference tasks that got scheduled with this API as “work”. Your scheduled work is intended to run reliably by using this API, even if the device restarted.
But sometimes you need to cancel your scheduled work. Within this article, we will have a quick look at the four methods for implementing this task with the WorkManager API.
Work cancellation
When you schedule your work, you have various possibilities to identify your scheduled work.
The first option is to use the id that will be automatically generated when you create a new work.
Cancel by ID
Canceling by id is probably the simplest way. The following code snippet shows you how to create a new OneTimeWorkRequest
for our dummy worker TestWorker
that represents an implementation of a ListenableWorker
class.