Canceling or updating ongoing work
Up until now, we have fired off a worker and waited for work to complete. However, what happens if you want to cancel the work? What happens if you want to update the requirements mid-work? For example, what if you want to stop music playback or a file download? What do you do if the backup task hasn’t run in days because the constraints were too restrictive, and you want to loosen them? In this section, we will discuss the mechanisms for aborting and updating ongoing work.
To cancel ongoing work, we need the WorkRequest ID. This is the same UUID that we used to collect updates for the work. With that ID and a WorkManager instance, we can call WorkManager.cancelWorkById(UUID). The work manager will then attempt to cancel the work. Cancellation is not guaranteed – ongoing work may still be executed.
To update work, Google recommends avoiding canceling the ongoing work and recreating it. This is because cancellation is not guaranteed...