How to bring up list of available notification sounds on Android
public void listRingtones() {
RingtoneManager manager = new RingtoneManager(this);
manager.setType(RingtoneManager.TYPE_NOTIFICATION);
// manager.setType(RingtoneManager.TYPE_RINGTONE);//For Get System Ringtone
Cursor cursor = manager.getCursor();
while (cursor.moveToNext()) {
String title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
String uri = manager.getRingtoneUri(cursor.getPosition());
String ringtoneName= cursor.getString(cursor.getColumnIndex("title"));
Log.e("All Data", "getNotifications: "+ title+"-=---"+uri+"------"+ringtoneName);
// Do something with the title and the URI of ringtone
}
}
Just copy/pasting some code from one of my apps that does what you are looking for.
This is in an onClick handler of a button labeled "set ringtone" or something similar:
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
this.startActivityForResult(intent, 5);
And this code captures the choice made by the user:
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
if (resultCode == Activity.RESULT_OK && requestCode == 5) {
Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null) {
this.chosenRingtone = uri.toString();
} else {
this.chosenRingtone = null;
}
}
}
Also, I advise my users to install the "Rings Extended" app from the Android Market. Then whenever this dialog is opened on their device, such as from my app or from the phone's settings menu, the user will have the additional choice of picking any of the mp3s stored on their device, not just the built in ringtones.
Or just stick this in your preferences XML:
<RingtonePreference android:showDefault="true"
android:key="Audio" android:title="Alarm Noise"
android:ringtoneType="notification" />
Full content of my sample XML just for context:
<?xml version="1.0" encoding="UTF-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference android:title="Some value"
android:key="someval"
android:summary="Please provide some value" />
<EditTextPreference android:title="Some other value"
android:key="someval2"
android:summary="Please provide some other value" />
<RingtonePreference android:showDefault="true"
android:key="Audio" android:title="Alarm Noise"
android:ringtoneType="notification" />
</PreferenceScreen>
This is the method I use to get a list of notification sounds available in the phone :)
public Map<String, String> getNotifications() {
RingtoneManager manager = new RingtoneManager(this);
manager.setType(RingtoneManager.TYPE_NOTIFICATION);
Cursor cursor = manager.getCursor();
Map<String, String> list = new HashMap<>();
while (cursor.moveToNext()) {
String notificationTitle = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
String notificationUri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX);
list.put(notificationTitle, notificationUri);
}
return list;
}
EDIT: This is for the comment regarding how to set the sound in the NotificationCompat.Builder. This method instead gets the ringtone's ID which is what the phone uses, instead of the human readable TITLE the other method got. Combine the uri and the id, and you have the ringtones location.
public ArrayList<String> getNotificationSounds() {
RingtoneManager manager = new RingtoneManager(this);
manager.setType(RingtoneManager.TYPE_NOTIFICATION);
Cursor cursor = manager.getCursor();
ArrayList<String> list = new ArrayList<>();
while (cursor.moveToNext()) {
String id = cursor.getString(RingtoneManager.ID_COLUMN_INDEX);
String uri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX);
list.add(uri + "/" + id);
}
return list;
}
The above code will return a list of strings like "content://media/internal/audio/media/27".. you can then pass one of these strings as a Uri into the .setSound() like:
.setSound(Uri.parse("content://media/internal/audio/media/27"))
Hope that was clear enough :)