Ringtone Preference defaults to Silent even though defaultValue set

随声附和 提交于 2019-12-24 05:57:58

问题


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?


回答1:


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);


来源:https://stackoverflow.com/questions/24071707/ringtone-preference-defaults-to-silent-even-though-defaultvalue-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!