Search code examples
androidandroid-download-manager

Customizing a download notification


Based on another SO answer, I am using the following lines of code to download media files:

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

Now, that is all good except that I have a few questions:

  1. The person who answered said that the notifications can be customized. How can that be done?
  2. If I want to add a "cancel" button to cancel the download and delete the file, how do I do it? What do I need to implement to get this behavior? :)

Solution

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

    Pretty self-explanatory.