我可以通过 XML 为 RingtonePreference 插入 android:defaultValue 特征吗?

发布于 2024-10-03 04:43:34 字数 572 浏览 5 评论 0原文

有没有办法通过 XML 在 RingtonePreference 中添加默认值?例如,我的首选项.xml 如下所示。

<RingtonePreference android:key="alarm"
android:title="Alarm" android:name="Alarm"
android:summary="Select an alarm"
android:ringtoneType="alarm" android:showDefault="true" />

如果可能的话,我想将 RingtonePreference 设置为默认铃声,如下所示:

<RingtonePreference android:key="alarm"
android:title="Alarm" android:name="Alarm"
android:summary="Select an alarm"
android:ringtoneType="alarm" android:showDefault="true"
android:defaultValue="defaultRingtone" />

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" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

来日方长 2024-10-10 04:43:34

您可以在 XML 中定义默认值。
正如你所说,它需要一个 URI。
只需输入您需要的默认 URI。
例如,对于默认警报声音,您可以输入:

<RingtonePreference android:key="alarm"
android:title="Alarm" android:name="Alarm"
android:summary="Select an alarm"
android:ringtoneType="alarm" android:showDefault="true"
android:defaultValue="content://settings/system/alarm_alert" />

对于通知,您可以输入:

android:defaultValue="content://settings/system/notification_sound"

等等。

You can define the default in the XML.
As you said, it needs a URI.
Simply put the default URI for what you need.
For example, for a default alarm sound you would put:

<RingtonePreference android:key="alarm"
android:title="Alarm" android:name="Alarm"
android:summary="Select an alarm"
android:ringtoneType="alarm" android:showDefault="true"
android:defaultValue="content://settings/system/alarm_alert" />

For a notification you would put:

android:defaultValue="content://settings/system/notification_sound"

Etc.

西瓜 2024-10-10 04:43:34

找到了一个解决方法,设置默认铃声。

对于同时使用 RingtonePreferencePreferenceManager.setDefaultValues() 的人,RingtonePreference 上的 android:defaultValue 需要在字符串中指向铃声的 URI。通过提供空字符串,您将默认首选项为“silence”,而其他字符串可能会导致没有有效的 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);
  }
}

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);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文