【问题标题】:Ringtone Preference defaults to Silent even though defaultValue set即使设置了 defaultValue,铃声偏好默认为静音
【发布时间】:2014-07-27 03:01:54
【问题描述】:

我正在使用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" />

...一堆其他偏好

Preferences 活动在基本 UI 中启动

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

铃声,app的主要功能启动时,在此取回

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_start 代码,则没有声音。

但是,这是奇怪的部分,如果用户启动 btn_settings(首选项屏幕),则选择 android:defaultValue,即使用户实际上并未启动铃声选择。换句话说,用户只需访问首选项屏幕即可选择默认值。如果用户不这样做,它就会保持沉默。

我错过了什么?

【问题讨论】:

    标签: android sharedpreferences default-value


    【解决方案1】:

    在访问 SharedPreferences 之前,您应该在 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      相关资源
      最近更新 更多