偏好选择始终保持默认
我遇到一个小问题,每次我尝试更改程序中的首选项时,它们在活动中都不会改变,它们只是保持默认值。
public class Reciever extends BroadcastReceiver {
boolean smsOn = false;
String smsColor = new String ("Green");
Uri smsSound;
String smsVibrate = new String ("Normal");
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(arg0);
smsOn = pref.getBoolean(Preferences."PREF_SMS_ON", false);
smsColor = pref.getString(Preferences.SMS_PREF_COLOR, "Green");
smsSound = Uri.parse(pref.getString(Preferences.SMS_PREF_SOUND, "Silent"));
smsVibrate = pref.getString(Preferences.SMS_PREF_SOUND, "Normal");
//all variable remain default value verified through debugger
NotificationManager mNotificationManager = (NotificationManager) arg0.getSystemService(Context.NOTIFICATION_SERVICE);
if (arg1.getAction().equals(ACTION)){
if(smsOn == true){....... if checkbox is checked smsOn still remains false
}
prefenceactivity class
public class Preferences extends PreferenceActivity implements OnPreferenceClickListener{
public static final String PREF_SMS_ON = "PREF_SMS_ON";
public static final String VIBRATE_ON_CALL1 = "VIBRATE_ON_CALL1";
public static final String SMS_PREF_COLOR = "SMS_PREF_COLOR";
public static final String SMS_PREF_SOUND = "SMS_PREF_SOUND";
public static final String SMS_PREF_VIB = "SMS_PREF_VIB";
}
xml 文件(我只会复制一个)
<CheckBoxPreference
android:key="PREF_SMS_ON"
android:title="SMS Notifications"
android:summary="Turn On SMS Notifications"
android:defaultValue="false">
</CheckBoxPreference>
我不明白它出了什么问题,这一切看起来都应该对我有用
I am having a small problem, every time i try to change the preferences in my program they never change in the activity, they just stay at what the default is.
public class Reciever extends BroadcastReceiver {
boolean smsOn = false;
String smsColor = new String ("Green");
Uri smsSound;
String smsVibrate = new String ("Normal");
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(arg0);
smsOn = pref.getBoolean(Preferences."PREF_SMS_ON", false);
smsColor = pref.getString(Preferences.SMS_PREF_COLOR, "Green");
smsSound = Uri.parse(pref.getString(Preferences.SMS_PREF_SOUND, "Silent"));
smsVibrate = pref.getString(Preferences.SMS_PREF_SOUND, "Normal");
//all variable remain default value verified through debugger
NotificationManager mNotificationManager = (NotificationManager) arg0.getSystemService(Context.NOTIFICATION_SERVICE);
if (arg1.getAction().equals(ACTION)){
if(smsOn == true){....... if checkbox is checked smsOn still remains false
}
prefenceactivity class
public class Preferences extends PreferenceActivity implements OnPreferenceClickListener{
public static final String PREF_SMS_ON = "PREF_SMS_ON";
public static final String VIBRATE_ON_CALL1 = "VIBRATE_ON_CALL1";
public static final String SMS_PREF_COLOR = "SMS_PREF_COLOR";
public static final String SMS_PREF_SOUND = "SMS_PREF_SOUND";
public static final String SMS_PREF_VIB = "SMS_PREF_VIB";
}
xml file(i will just copy one)
<CheckBoxPreference
android:key="PREF_SMS_ON"
android:title="SMS Notifications"
android:summary="Turn On SMS Notifications"
android:defaultValue="false">
</CheckBoxPreference>
i dont understand what it wrong, it all looks like it should work to me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,您的代码存在一些问题。
这个教程非常适合创建首选项。
另外,如果您想更改
PreferenceActivity
之外的首选项,您应该使用编辑器例如:
我希望它能帮助
很好地回复您的评论
,问题是您正在使用
PreferenceActivity
,如 此处用于显示首选项的视觉样式。另外,正如此处所示,“首选项将自动保存到SharedPreferences
当用户与它们交互时”。它给我的印象是您想要更改PreferenceActivity
之外的首选项(这并没有错),但您说那不是您想要做的。但是,我确实注意到您的
PreferenceActivity
没有从资源文件加载首选项,因为您需要在addPreferencesFromResource(R.xml.settings);
添加addPreferencesFromResource(R.xml.settings);
例如,代码>onCreate。但是,就像我说的,只有当用户直接与此活动交互时,您的首选项才会自动更改。我希望这有帮助。
Well, there are some problems in your code.
This tutorial is good for creating preferences.
Also, if you want to change the preferences outside the
PreferenceActivity
you should use the EditorFor example:
I hope it helps
The reply to your comment
well, the thing is that you are using
PreferenceActivity
, which as the documentation show in here it is used for showing a visual style of the preferences. Also, as it is indicated here, "the preferences will automatically save toSharedPreferences
as the user interacts with them". It gave me the impression that you wanted to change preferences outside thePreferenceActivity
(which is not wrong), but you say thats not what you are wanting to do.However, I do notice that your
PreferenceActivity
is not loading the preferences from your resource file, for that you need to addaddPreferencesFromResource(R.xml.settings);
in youronCreate
, for example. But, like I said, your preferences will only change automatically if the user interacts with this activity directly.I hope that helps.