自定义下载通知

时间:2014-08-01 12:16:46

标签: android android-download-manager

基于另一个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);  

现在,除了我有几个问题之外,这一切都很好:

  1. 回答的人说可以自定义通知。 怎么办?
  2. 如果我想添加“取消”按钮取消下载并删除文件,我该怎么办?为了获得这种行为,我需要实现什么? :)

1 个答案:

答案 0 :(得分:2)

// 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);  

非常不言自明。