问题描述
有没有办法来添加在铃声preference默认值,通过XML?举例来说,这里就是我的preference.xml的样子。
<铃声preference安卓键=报警
机器人:标题=报警机器人:NAME =报警
机器人:总结=选择报警
机器人:ringtoneType =报警机器人:showDefault =真/>
如果有可能,我想设定铃声preference为默认铃声,像这样的:
<铃声preference安卓键=报警
机器人:标题=报警机器人:NAME =报警
机器人:总结=选择报警
机器人:ringtoneType =报警机器人:showDefault =真
安卓:设置defaultValue =defaultRingtone/>
想出了一个变通办法,在设置默认铃声。
有关谁使用人既是铃声preference
和 preferenceManager.setDefaultValues()
,安卓设置defaultValue
在铃声preference
接受一个字符串铃声的URI。通过提供一个空字符串,你拖欠preference为沉默,而其他的字符串可能会导致没有有效的URI。
的变通,那么,是提供一个虚假的字符串,如安卓设置defaultValue =defaultRingtone
:
<铃声preference安卓键=报警
机器人:标题=报警机器人:NAME =报警
机器人:总结=选择报警
机器人:ringtoneType =报警机器人:showDefault =真
安卓:设置defaultValue =defaultRingtone/>
在调用 preferenceManager.setDefaultValues()
,抢preference,并检查假的串被存储:
//设置在options.xml中定义的存储preferences为默认值
preferenceManager.setDefaultValues(这一点,R.layout.options,假);
//检查存储的字符串值,将Rington preference标记下
最终的共享preferences设置= preferenceManager.getDefaultShared preferences(本);
最后弦乐savedUri = savedState.getString(报警,);
//默认情况下,设置报警的URI为null
乌里alarmUri = NULL;
//检查实际上是提供了一个字符串
如果(savedUri.length()大于0){
//如果存储的字符串是假的串...
如果(savedUri.equals(defaultRingtone)){
//设置报警该系统的默认报警。
alarmUri = Settings.System.DEFAULT_ALARM_ALERT_URI;
//保存此警报的字符串,这样我们就不必经历这又
最终的共享preferences.Editor saveEditor = saveState.edit();
saveEditor.putString(报警,alarmUri.toString());
saveEditor.commit();
}
//否则,检索URI正常。
其他 {
alarmUri = Uri.parse(savedUri);
}
}
Is there a way to add a default value in a RingtonePreference, via XML? For example, here's what my preference.xml looks like.
<RingtonePreference android:key="alarm"
android:title="Alarm" android:name="Alarm"
android:summary="Select an alarm"
android:ringtoneType="alarm" android:showDefault="true" />
If possible, I'd like to set the RingtonePreference to the default ringtone, like this:
<RingtonePreference android:key="alarm"
android:title="Alarm" android:name="Alarm"
android:summary="Select an alarm"
android:ringtoneType="alarm" android:showDefault="true"
android:defaultValue="defaultRingtone" />
Figured out a work-around, in setting the default ringtone.
For the people who uses both a RingtonePreference
and PreferenceManager.setDefaultValues()
, android:defaultValue
on a RingtonePreference
takes in a string to a ringtone's URI. By providing an empty string, you're defaulting the preference to "silence," while other strings will probably lead to no valid URI.
The work-around, then, is to provide a bogus string, such as 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" />
When calling PreferenceManager.setDefaultValues()
, grab the preference, and check if the bogus string is being stored:
// 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);
}
}
这篇关于我可以插入一个机器人:设置defaultValue特质的铃声preference,通过XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!