I am using DownloadManager to download a bunch of files in my application. I am not able to figure out how to cancel the downloads which has been enqueued by downloadManager.
There are two possibilities: a. User can manually cancel it say by clicking it in the notification bar. b. Cancel and remove the download through code.
I have the following receiver defined.
<receiver android:name=".DownloadStatusReceiver" android:exported="true"> <intent-filter> <action android:name="android.intent.action.DOWNLOAD_COMPLETE" /> <action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED" /> </intent-filter> </receiver>
And in the receiver
if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)) { Constants.showLog(TAG, "Notification clicked"); long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0); DownloadManager dm =(DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE); dm.remove(downloadId); }
Any insights?
To stop a web download, swipe down from the top of the screen and select Pause or Cancel. To stop a Play Store download, tap X on the progress bar.
Force the Play Store to close: Go to Settings > Apps & Notifications > See all # apps. Tap Google Play Store > Force Stop.
Open settings, go to apps, running apps, if you dont see download manager then scroll down untill you see it, click on it.. and click on force stop.. it will stops the app and closes current downloading task.
You can cancel downloads via DownloadManager
by calling its remove(long...)
method. For this you need the ID of the download. From my experience there basically are two reliable ways how to get it:
enqueue(DownloadManager.Request)
method.DownloadManager
for downloads via query(DownloadManager.Query)
method. Then retrieve the IDs from the returned Cursor
, they are stored in the column named DownloadManager.COLUMN_ID
.Broadcast Receiver
From my experience, it is not reliable to retrieve download ID via BroadcastReceiver
for action android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED
(though the broadcast is always sent).
DownloadManager. EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS
does not work properly. On some devices, it always return null. If it returns something on some devices, it is the ID of the download started first. And if the first download is finished/canceled, it returns null for notification of the remaining downloads. DownloadManager.EXTRA_DOWNLOAD_ID
does not work for this action.Getting ID in broadcast for action android.intent.action.DOWNLOAD_COMPLETE
seems reliable. You have to get it from extra DownloadManager.EXTRA_DOWNLOAD_ID
. Note that the broadcast is sent not only for completed download, it is also sent when you cancel download calling remove()
.
Note: Downloads are sometimes grouped in one notification, sometimes create multiple notifications. I wasn't able to figure out the conditions when notifications do and do not group. It seems to depend on many factors like OS version, device, download title, ... and in general seems rather unpredictable.
Note: I've tested whether you can cancel other app's download and it doesn't seem so. Even though that the IDs are database IDs that are unique across all apps. Calling remove()
does not cancel another app's download.
If you are looking how to stop the download, then I am sure you know how to download data through URL.
I am hopping you are familiar with point 1, point 2 and point 3, and your solution is point 4.
private var mgr: DownloadManager? = null
private var enqueue: Long? = null
enqueue = mgr?.enqueue(request)
mgr?.remove(enqueue!!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With