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?
remove()
should work, according to the docs. Of course, you do not seem to be callingremove()
until after the download is completed. That's like locking the barn after the horse has bolted. :-)remove()
from something more under your control, like aButton
or an action bar item. Also, did you confirm thatdownloadId
is coming back with a value, one that matches your original request?