1

I am using DownloadManager to download a xml file from a URL. It works fine but I have two questions:

1.) How can I show a message about the download in the closed notification bar? I can show a message when I open the bar like shown in this snapshot: NotificationBar

2.) How can I programmatically remove tis notification?

My code for the DownloadManager:

            //Download XML file from URL 
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(URL));
        request.setTitle("Download von "+Name+".xml");
        request.setDescription("Download von "+Name+".xml");

        // in order for this if to run, you must use the android 3.2 to compile your app
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        }

        request.setDestinationInExternalPublicDir(FileSeperator+"XML"+FileSeperator, Name + FileExtension);

        // get download service and enqueue file
        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        manager.enqueue(request);  
Mokkapps
  • 2,028
  • 9
  • 42
  • 67

4 Answers4

3

The DownloadManager API provides a remove(long... ids) method that lets you delete the downloaded file from the filesystem. remove() deletes the file from both the filesystem and the Download Manager app's list of downloaded files. It also removes the progress notification from the system notification drawer. Just pass it the ID you received when you enqueued your request.

What might be a bit surprising is that there may also be a completion notification, and if your download completes successfully, DownloadManager does not delete the completion notification, even if you call remove() with your download request ID.

Even if the user deletes the file from the Download Manager app, it will also remove only the progress notification from the system notification drawer, but the completion notification still persists. And if the user has removed the app manually, or if you have removed the download programmatically (via remove()), then the notification causes a crash in the OS, with a report of "Unfortunately, the process android.process.media has stopped." This seems to be an Android bug.

Even the NotificationManager API doesn't help because DownloadManager owns the completion notification, not your app. So cancelAll() won't have any effect.

Bottom line: if you don't want the completion notification, don't call setNotificationVisibility() on your request with the value VISIBILITY_VISIBLE_NOTIFY_COMPLETED. The default visibility is VISIBILITY_VISIBLE, which only displays the progress notification. And as I've described, that progress notification goes away automatically.

If you want total control over notifications, you can set your request to use VISIBILITY_HIDDEN (which requires the permission android.permission.DOWNLOAD_WITHOUT_NOTIFICATION). And then you can display whatever custom notifications you'd like in the usual way with the NotificationManager API.

For my app, where I'm downloading a jar file and then extracting its contents and removing the jar file, I settled on VISIBILITY_VISIBLE (with no completion notification). Here's how I create the request:

Request request = new Request(baseUrl + destinationFile))
            .setDescription("file description")
            .setDestinationInExternalFilesDir(mContext, null, destinationFile)
            .setNotificationVisibility(Request.VISIBILITY_VISIBLE)
            .setVisibleInDownloadsUi(true)
            .setTitle("My App Title");
Steve Liddle
  • 3,685
  • 3
  • 27
  • 39
1

Yes, you can show a notification when the download is complete. You can remove it too. Follow this post How can I programmatically open/close notifications in Android?

Community
  • 1
  • 1
Royston Pinto
  • 6,681
  • 2
  • 28
  • 46
  • in your post I can only find code to programmatically expand the notification bar. That is not what I am looking for. I want to remove the notification, I do not need to expand the bar. – Mokkapps Oct 09 '12 at 10:16
  • Try this one, http://stackoverflow.com/questions/8841078/clear-a-status-notification-android?rq=1 – Royston Pinto Oct 09 '12 at 10:19
0

You can use following function at onReceive method of broadcastReceiver


downloadManager.addCompletedDownload(YOUR_TITLE, YOUR_DESCRIPTION, true, YOUR_MIMETYPE, downloadedFile.getPath(), downloadedFile.getTotalSpace(), false);


BroadcastReceiver like this:

registerReceiver(broadcastReceiver, new IntentFilter("android.intent.action.DOWNLOAD_COMPLETE"));


If you set the last parameter to "true" the notification will be shown if download is complete. If set to "false" the notification is only displayed while downloading and if download is complete and notification is canceled automatically

-1

Notification overview. Method notify. Method cancel.

Jin35
  • 8,602
  • 3
  • 32
  • 52
  • I try to run in this code in a OnCreate() method after downloading my file `NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.cancelAll(); ` But the notifications are not removed... Is it because the notifications are created from the DownloadManager? – Mokkapps Oct 09 '12 at 11:03
  • What you want to do? Remove notification when it's clicked? – Jin35 Oct 09 '12 at 11:06
  • For removing your own notification use `cancel(id)`, where `id` is some number you provide in `notify(id, notification)`. – Jin35 Oct 09 '12 at 11:07
  • No the notification created by the DownloadManager (code shown in my main post) should be removed programmatically in a onCreate() method of a activity. Do you know how I can add an id to a notification from DownloadManager? – Mokkapps Oct 09 '12 at 11:08
  • I suppose you can't remove third party notification (without root permissions). If DownloadManager does not provide this API it can't be done. – Jin35 Oct 09 '12 at 11:17
  • Ok I already assumed that, thanks! If someone finds a way to realize it, please post it here! – Mokkapps Oct 09 '12 at 11:24