我已经为下载进度条完成了此代码,但如果用户不小心单击了按钮,我也想取消该下载任务。 我无法实现这一点,任何人都可以帮助我摆脱困境。
提示:我是Android Java的新手,所以请用所有详细的答案来解释它。
String url = "URL";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Some descrition");
request.setTitle("Some title");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "PDF_demo.ext");
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
if (manager != null) {
long downloadID = manager.enqueue(request);
}
从文档中,尝试使用:
DownloadManager.remove(downloadID)