5

我正在使用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 做些什么吗?

4

1 回答 1

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)会从内存中删除文件。我必须添加额外的代码才能将文件永久保存在我希望它最初保存的位置。

于 2013-01-22T10:01:42.840 回答