1

我正在使用 DownloadManager 从 URL 下载 xml 文件。它工作正常,但我有两个问题:

1.) 如何在关闭的通知栏中显示有关下载的消息?我可以在打开栏时显示一条消息,如下图所示:通知栏

2.) 如何以编程方式删除 tis 通知?

我的下载管理器代码:

            //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);  
4

4 回答 4

3

DownloadManagerAPI提供了一种方法,remove(long... ids)可让您从文件系统中删除下载的文件。 remove()从文件系统和下载管理器应用程序的下载文件列表中删除文件。它还会从系统通知抽屉中删除进度通知。只需将您在排队请求时收到的 ID 传递给它。

可能有点奇怪的是,可能还会有完成通知,如果你的下载成功完成,DownloadManager 不会删除完成通知,即使你remove()用你的下载请求 ID 调用。

即使用户从下载管理器应用程序中删除文件,它也只会从系统通知抽屉中删除进度通知,但完成通知仍然存在。如果用户手动删除了应用程序,或者您以编程方式(通过remove())删除了下载,则通知会导致操作系统崩溃,并报告“不幸的是,进程 android.process.media 已停止”。 这似乎是一个Android错误。

甚至NotificationManagerAPI也无济于事,因为DownloadManager拥有完成通知,而不是您的 app。所以cancelAll()不会有任何影响。

底线:如果您不想要完成通知,请不要setNotificationVisibility()使用 value 调用您的请求VISIBILITY_VISIBLE_NOTIFY_COMPLETED。默认可见性为VISIBILITY_VISIBLE,仅显示进度通知。正如我所描述的,进度通知会自动消失。

如果您想完全控制通知,您可以将您的请求设置为使用VISIBILITY_HIDDEN(需要权限android.permission.DOWNLOAD_WITHOUT_NOTIFICATION)。NotificationManager然后您可以使用API以通常的方式显示您想要的任何自定义通知。

对于我的应用程序,我正在下载一个 jar 文件,然后提取其内容并删除 jar 文件,我选择了VISIBILITY_VISIBLE(没有完成通知)。这是我创建请求的方式:

Request request = new Request(baseUrl + destinationFile))
            .setDescription("file description")
            .setDestinationInExternalFilesDir(mContext, null, destinationFile)
            .setNotificationVisibility(Request.VISIBILITY_VISIBLE)
            .setVisibleInDownloadsUi(true)
            .setTitle("My App Title");
于 2014-07-19T17:23:38.333 回答
1

是的,您可以在下载完成时显示通知。你也可以删除它。关注这篇文章如何以编程方式在 Android 中打开/关闭通知?

于 2012-10-09T10:09:05.197 回答
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

于 2015-08-13T12:51:18.080 回答
-1

通知概述。方法通知。方法取消

于 2012-10-09T10:21:59.160 回答