我正在使用下载管理器
从我的服务器下载图像。
它可以很好地下载文件并将其放在我想要的位置。但出于某种原因,通知一直存在,我似乎无法删除它。下载管理器的代码如下:
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 完成后我可以做些什么吗?
最佳答案
我设法解决了我的问题。在 BroadcastReceiver
中,我必须从 Intent 中获取下载 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)
,这将从内存中删除文件。我必须添加额外的代码才能将文件永久保存在我最初想要保存的位置。
关于Android:DownloadManager - 通知粘贴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14443536/