Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bring up list of available notification sounds on Android

I'm creating notifications in my Android application, and would like to have an option in my preferences to set what sound is used for the notification. I know that in the Settings application you can choose a default notification sound from a list. Where does that list come from, and is there a way for me to display the same list in my application?

like image 417
robintw Avatar asked Apr 27 '10 20:04

robintw


2 Answers

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.

like image 69
Mark B Avatar answered Nov 11 '22 09:11

Mark B


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>
like image 50
JD. Avatar answered Nov 11 '22 08:11

JD.