Android 铃声首选项默认为静音,即使已设置defaultValue

Android 铃声首选项默认为静音,即使已设置defaultValue,android,sharedpreferences,default-value,Android,Sharedpreferences,Default Value,我使用preferences.xml允许选择铃声(以及其他首选项)。这是密码 <RingtonePreference android:key="shuttle_tone" android:ringtoneType="notification" android:showDefault="false" android:showSilent="true" android:defaultValue="content://settings/system/notifica

我使用preferences.xml允许选择铃声(以及其他首选项)。这是密码

<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" />
当应用程序的主要功能启动时,将在此处检索铃声

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 */
问题是:当程序第一次安装时,如果用户在没有访问首选项屏幕的情况下启动run_启动代码,则不会有声音

然而,这是奇怪的部分,如果用户启动btn_设置(首选项屏幕),则会选择android:defaultValue,即使用户实际上没有启动铃声选择。换句话说,用户只需访问preferences屏幕即可选择defaultValue。如果用户不这样做,它将保持沉默


我错过了什么

在访问SharedReference之前,应在onCreate中添加以下内容:

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