******现在已经完成了代码,但取消后通知显示再次运行!
我正在使用DownloadManager通过url下载文件,它工作正常。 代码创建管理器:
String[] url = {urlsProz,urlsMb};
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url[i]));
downloadFile=url[i];
request.setDescription("Some description");
request.setTitle("Some titles");
// 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);
}
String s= Environment.DIRECTORY_DOWNLOADS;
Log.i("*** testDownloadManager***",s+"/"+url[i]);
request.setDestinationInExternalPublicDir(s, "name-of-the-file"+(++j)+".ext");
// get download service and enqueue file
manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
//Download progress will be showing in the notification bar
downloadId=manager.enqueue(request);
Log.i("*** testDownloadManager***","downloadId:"+downloadId);
按下取消按钮后的代码:
if (manager != null) {
//remove() method will return the number of downloads removed
//Any downloaded files (complete or partial) will be deleted
int i = manager.remove(downloadId);
Log.i(TAG, downloadId + "," + i + " cancelled");
NotificationManager notifManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
//notifManager.cancelAll(); //doesn'nt work
manager = null;
}
管理员已取消,但即使在应用程序之后通知显示仍在运行。 有任何想法取消此通知吗?
关心Wicki
答案 0 :(得分:0)
cancelAll()方法仅取消应用程序创建的通知。
我认为您使用DownloadManager:
// start download
Request request = new Request(Uri.parse(urltoDownload));
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
request.setTitle("my title");
long enqueue = downloadManager.enqueue(request);
所以它创建了通知的DownloadManager应用程序(服务),它只有DownloadManager应用程序可以取消它。 您无法取消其他应用通知,但这是不可能的。
答案 1 :(得分:0)
DownloadManager dm = (DownloadManager) c
.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request dlrequest = new DownloadManager.Request(
Uri.parse(url));
dlrequest
.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setTitle("Downloading")
.setDescription("Downloading in Progress..")
.setDestinationInExternalPublicDir("folder_name", name + ".jpg")
**.setNotificationVisibility(visibility)**
.allowScanningByMediaScanner();
dm.enqueue(dlrequest);
.setNotificationVisibility(visibility) - >设置可见性为true或false。并完成了......!