Question

I've currently got a regular notification that tracks the progress of a download (of a song). Once the download is complete I'd like to make it so that if the user presses on the notification it opens up the song file in the default application that would handle that file type.

I've tried searching but I'm unsure as to how to work with the PendingIntent and the MIME type when using .setDataAndType(). Could someone please explain or provide some example code for how I could set up the intent to open up the file from a specific file path in the default app?

Thank you, Daniel

Was it helpful?

Solution

Create the notification like this with the pending intent to open the default audio player

    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    File file = new File("YOUR_SONG_URI"); // set your audio path 
    intent.setDataAndType(Uri.fromFile(file), "audio/*");

    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

    Notification noti = new NotificationCompat.Builder(this)
            .setContentTitle("Download completed")
            .setContentText("Song name")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent).build();

    noti.flags |= Notification.FLAG_AUTO_CANCEL;

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0, noti);

OTHER TIPS

please put this code inside your notification code

Intent intent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("content://media/internal/images/media"));

Change your Uri path to directory where songs are placed

Have you tried this?

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File("/sdcard/download/path/test.mp3");
intent.setDataAndType(Uri.fromFile(file), "audio/*");

this intent should be the open action of the notification. Since you said song so MIME will be audio/* or else you can use AndroidMimeTypeMap to get the mime type from file extention.

I have used this code it opens the "open action using" window. If you have set default app on your phone or tablet it will open the default app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top