铃声preference默认为静音即使设置defaultValue集 [英] Ringtone Preference defaults to Silent even though defaultValue set

查看:175
本文介绍了铃声preference默认为静音即使设置defaultValue集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用preferences.xml让铃声的选择(其它preferences之间)。这里的code

I'm using preferences.xml to allow selection of a ringtone (among other preferences). Here's the code

<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的

... a bunch of other preferences

在preferences活动中的基本UI开始与

The Preferences activity is initiated in the base UI with

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

铃声,当应用程序启动的主要功能,在这里检索

The ringtone, when the main function of the app starts, is retrieved here

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 code无需访问preferences屏幕,没有声音

Here's the problem: When the program is FIRST INSTALLED, if the user initiates the run_start code without visiting the preferences screen, there's no sound.

然而,这是怪异的一部分,如果用户启动btn_settings(preferences屏),在android:设置defaultValue选择,即使用户没有实际启动铃声选择。换句话说,用户只需具有访问preferences画面要选择的设置defaultValue。如果用户没有,这是无声的。

However, and this is the weird part, if the user initiates btn_settings (preferences screen), the android:defaultValue is selected, even if the user DOES NOT ACTUALLY INITIATE SELECTION OF THE RINGTONE. In other words, the user just has to visit the preferences screen for the defaultValue to be selected. If the user doesn't, it's silent.

我在想什么?

推荐答案

在访问共享preferences之前,您应该添加以下中的onCreate:

You should add the following in onCreate before you access the SharedPreferences:

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

这篇关于铃声preference默认为静音即使设置defaultValue集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆