android - 自定义下载通知

标签 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. 如果我想添加一个“取消”按钮来取消下载并删除文件,我该怎么做?我需要实现什么才能获得此行为? :)

最佳答案

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

相关文章:

java - android ksoap2 java.lang.IllegalArgumentException : size <= 0

android - NotesDbAdapter 示例是针对一张表的,那么多张表呢?

Android Intent 过滤器在尝试在下载管理器中查看 CSV 文件时收到通知

android - DownloadManager IllegalStateException 在 DIRECTORY_DOWNLOADS 中创建下载

android - 如何以编程方式获取用户在 Android OS 配置上设置的数据使用限制?

android - 用于在 android 中取消下载的广播接收器

java - 如何在单击列表中父级的特定项目时扩展子级?

Android:将参数传递给警报对话框

java - Android - DownloadManager - 清除队列中的旧下载

java - 如何在java中向appBarLayout添加后退导航图标