22

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?

5
  • remove() should work, according to the docs. Of course, you do not seem to be calling remove() until after the download is completed. That's like locking the barn after the horse has bolted. :-) Dec 28, 2012 at 18:46
  • I would implement my own downloading workflow, because the built-in download manager is buggy and doesn't work on old devices.
    – vortexwolf
    Dec 28, 2012 at 19:34
  • @CommonsWare: I am calling remove when the notification bar is clicked. I assume the notification click event comes through whether the download is complete or not.
    – PravinCG
    Dec 29, 2012 at 5:10
  • Beats me. I haven't played much with the notification clicks. Try experimenting with remove() from something more under your control, like a Button or an action bar item. Also, did you confirm that downloadId is coming back with a value, one that matches your original request? Dec 29, 2012 at 13:31
  • I fixed it. For me I was downloading multiple files and they get bunched up together on the same progress bar. So now as soon as I receive the notification clicked broadcast, I query and iterate through the Ids and remove them one by one. Works. The only issue which I am not sure of is will it also remove downloads not started by my application.
    – PravinCG
    Dec 29, 2012 at 16:00

2 Answers 2

58

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:

  1. Remember the return value of enqueue(DownloadManager.Request) method.
  2. Query the 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).

  1. Getting download IDs from extra 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.
  2. Getting a value from extra 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.

5
  • 1
    Thanks Tomik, you have summarized it well and I concur that I have observed the same. I am accepting this answer as it would help others in future.
    – PravinCG
    Dec 30, 2012 at 6:37
  • very useful. giving personal experience is always most valuable things one can offer, you save lots of time.
    – mmlooloo
    Sep 16, 2014 at 6:27
  • 3
    @Tomik, so what's actually the best way to get the id on DOWNLOAD_NOTIFICATION_CLICKED? I'm downloading several files at the same time, can't keep all ids. Nov 26, 2014 at 23:04
  • What if I don't know ID, because it was started by another app or instance? How to stap all downloads?
    – djdance
    Aug 11, 2017 at 13:06
  • Your answer is correct. but how start that download again. do you have any full source as an example. Aug 28, 2017 at 4:02
1

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.

  1. private var mgr: DownloadManager? = null
  2. private var enqueue: Long? = null
  3. enqueue = mgr?.enqueue(request)
  4. mgr?.remove(enqueue!!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.