RudyF
RudyF

Reputation: 825

Ringtone Preference defaults to Silent even though defaultValue set

I'm using preferences.xml to allow selection of a ringtone (among other preferences). Here's the code

<RingtonePreference
   android:key="shuttle_tone"
   android:ringtoneType="notification"
   android:showDefault="false"
   android:showSilent="true"
   android:defaultValue="content://settings/system/notification_sound"
   android:summary="Select the tone that signals the start of the next shuttle"
   android:title="Shuttle Tone" />

... a bunch of other preferences

The Preferences activity is initiated in the base UI with

case R.id.btn_settings:
   Intent settingsActivity = new Intent(getBaseContext(), PreferenceSelect.class);
   startActivity(settingsActivity);

The ringtone, when the main function of the app starts, is retrieved here

case R.id.run_start:
   // Retrieve the shuttle & level notifications
   SharedPreferences preference = PreferenceManager
         .getDefaultSharedPreferences(getApplicationContext());
   String strRingtonePreference = preference.getString(
         "shuttle_tone", "DEFAULT_SOUND");
   shuttle_rt = RingtoneManager.getRingtone(
         getApplicationContext(),
         Uri.parse(strRingtonePreference));

   /* More stuff ... during which the ringtone is played periodically */

Here's the problem: When the program is FIRST INSTALLED, if the user initiates the run_start code without visiting the preferences screen, there's no sound.

However, and this is the weird part, if the user initiates btn_settings (preferences screen), the android:defaultValue is selected, even if the user DOES NOT ACTUALLY INITIATE SELECTION OF THE RINGTONE. In other words, the user just has to visit the preferences screen for the defaultValue to be selected. If the user doesn't, it's silent.

What am I missing?

Upvotes: 1

Views: 730

Answers (1)

todd
todd

Reputation: 1286

You should add the following in onCreate before you access the SharedPreferences:

// Initialize the preferences with default settings if 
// this is the first time the application is ever opened
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

Upvotes: 2

Related Questions