想出了一个解决方法,设置默认铃声。
对于同时使用 aRingtonePreference
和的人PreferenceManager.setDefaultValues()
,android:defaultValue
在 a 上将RingtonePreference
字符串接收到铃声的 URI。通过提供一个空字符串,您将默认设置为“静音”,而其他字符串可能会导致没有有效的 URI。
那么,解决方法是提供一个伪造的字符串,例如android:defaultValue="defaultRingtone"
:
<RingtonePreference android:key="alarm"
android:title="Alarm" android:name="Alarm"
android:summary="Select an alarm"
android:ringtoneType="alarm" android:showDefault="true"
android:defaultValue="defaultRingtone" />
调用时PreferenceManager.setDefaultValues()
,获取首选项,并检查是否存储了虚假字符串:
// Set the stored preferences to default values defined in options.xml
PreferenceManager.setDefaultValues(this, R.layout.options, false);
// Check the stored string value, under the RingtonPreference tag
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
final String savedUri = savedState.getString("alarm", "");
// By default, set the alarm's URI to null
Uri alarmUri = null;
// Check if a String was actually provided
if(savedUri.length() > 0) {
// If the stored string is the bogus string...
if(savedUri.equals("defaultRingtone")) {
// Set the alarm to this system's default alarm.
alarmUri = Settings.System.DEFAULT_ALARM_ALERT_URI;
// Save this alarm's string, so that we don't have to go through this again
final SharedPreferences.Editor saveEditor = saveState.edit();
saveEditor.putString("alarm", alarmUri.toString());
saveEditor.commit();
}
// Otherwise, retrieve the URI as normal.
else {
alarmUri = Uri.parse(savedUri);
}
}