My use case is simple. Upload multiple images in the background and show the progress in a notification. Now I want to cancel this task when the user presses the cancel button in the notification. As the upload task cannot be serialized then how am I supposed to cancel that particular upload task?
Upload multiple images in the background and show the progress in a notification.
For those operations, I assume you are using a line of code similar to this:
UploadTask uploadTask = storageReference.putFile(filePath);
Now I want to cancel this task when the user presses the cancel button in the notification.
As seen the above line of code, the putFile(Uri uri) method that is called on a StorageReference object, returns an object of type UploadTask . This class is a subclass of StorageTask , which in terms is a subclass of ControllableTask , which is also a subclass of CancellableTask .
Because of the inheritance relationship between these classes, you can simply call CancellableTask's cancel() method:
Attempts to cancel the task.
In order to cancel the Task. In code should look like this:
uploadTask.cancel();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.