Android:DownloadManager - Notification Sticking

时间:2013-01-21 16:57:28

标签: android android-download-manager

我正在使用Download Manager从我的服务器下载图像。

下载文件并将其放在我想要的位置。但由于某种原因通知坚持,我似乎无法删除它。下载管理器的代码如下:

mDownloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

Uri uri = Uri.parse("URL"));

long enqueue = mDownloadManager.enqueue(new DownloadManager.Request(uri)
            .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI)
            .setAllowedOverRoaming(false)
            .setTitle("Title")
            .setDescription("File description")
            .setDestinationInExternalPublicDir("Folder", "Filename")
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE));

BroadcastReceiver onComplete = new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {
        Toast.makeText(getApplicationContext(), "Download Completed", Toast.LENGTH_SHORT).show();
    }
 };

如何在下载通知后删除通知

我尝试过设置所有不同的通知可见性模式而没有运气。一旦完成,我可以从BroadcastReceiver做些什么吗?

1 个答案:

答案 0 :(得分:8)

我设法解决了我的问题。在BroadcastReceiver我必须从意图中获取下载ID并从DownloadManager中删除。

BroadcastReceiver onComplete = new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {
        Toast.makeText(getApplicationContext(), "Download Completed", Toast.LENGTH_SHORT).show();

        // Get the download_id of the completed download.
        long download_id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);

        // Remove the completed download from the DownloadManager
        mDownloadManager.remove(download_id);
    }
 };

我还要注意,通过执行mDownloadManager.remove(download_id),这将从内存中删除该文件。我不得不添加额外的代码以永久保存文件在我希望它最初保存的位置。