根据另一个 SO 答案,我使用以下代码行来下载媒体文件:
DownloadManager.Request r = new DownloadManager.Request(Uri.parse(uri));
// This put the download in the same Download dir the browser uses
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_PODCASTS, fileName);
// When downloading music and videos they will be listed in the player
// (Seems to be available since Honeycomb only)
r.allowScanningByMediaScanner();
// Notify user when download is completed
// (Seems to be available since Honeycomb only)
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// Start download
DownloadManager dm = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(r);
现在,一切都很好,除了我有几个问题:
- 回答的人说可以自定义通知。 如何做到这一点?
- 如果我想添加一个“取消”按钮来取消下载并删除文件,我该怎么做?我需要实现什么才能获得此行为? :)
最佳答案
// Changing the Notification Title, Description, and its Visibility
DownloadManager mgr = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("///.mp3");
DownloadManager.Request req = DownloadManager.Request(uri);
req.setTitle("Download");
req.setDescription("Kick Ass Description");
req.setVisibility(Request.VISIBILITY_VISIBLE_COMPLETION_ONLY);
非常不言自明。
关于android - 自定义下载通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25080086/