Understanding Ringtone Preference

RingtonePreference deals specifically with ringtones. You'd use it in an application that gives the user an option to select a ringtone as a preference. Figure 11-5 shows the UI of the RingtonePreference example and Listing 11-6 shows the XML.

Figure 11-5. The RingtonePreference example UI

Listing 11-6. Defining a RingtonePreference Preference

// ringtone.xml (store at res/xml/ringtone.xml) <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:key="flight_option_preference" android:title="My Preferences" android:summary="Set Flight Option Preferences'^ <RingtonePreference android:key="ring_tone_pref" android:title="Set Ringtone Preference" android:showSilent="true" android:ringtoneType="alarm" android:summary="Set Ringtone" /> </PreferenceScreen>

// RingtonePreferenceActivity.java import android.os.Bundle;

import android.preference.PreferenceActivity;

public class RingtonePreferenceActivity extends PreferenceActivity {

^Override protected void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); addPreferencesFromResource(R.xml.ringtone);

When the user selects "Set Ringtone Preference," the preferences framework displays a ListPreference containing the ringtones on the device (see Figure 11-5). The user can then select a ringtone and then choose OK or Cancel. If he clicks OK, the selection is persisted to the preference store. Note that with the ringtones, the value stored in the preference store is the URI of the selected ringtone—unless he selects "Silent," in which case the stored value is an empty string. An example URI looks like this:

<string name="ring_tone_pref">content://media/internal/audio/media/26</string>

Finally, the RingtonePreference shown in Listing 11-6 follows the same pattern as the other preferences you've defined thus far. The difference here is that you set a few different attributes, including showSilent and ringtoneType. You can use showSilent to include the silent ringtone in the ringtone list, and ringtoneType to restrict the types of ringtones displayed in the list. Possible values for this property include ringtone, notification, alarm, and all.

0 0

Post a comment