偏好选择始终保持默认

发布于 2024-10-17 04:35:42 字数 1741 浏览 7 评论 0原文

我遇到一个小问题,每次我尝试更改程序中的首选项时,它们在活动中都不会改变,它们只是保持默认值。

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 技术交流群。

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

发布评论

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

评论(1

末骤雨初歇 2024-10-24 04:35:42

嗯,您的代码存在一些问题。
这个教程非常适合创建首选项。
另外,如果您想更改 PreferenceActivity 之外的首选项,您应该使用编辑器
例如:

Editor e = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
e.putBoolean(Preferences."PREF_SMS_ON", true);
e.commit();

我希望它能帮助

很好地回复您的评论

,问题是您正在使用 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 Editor
For example:

Editor e = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
e.putBoolean(Preferences."PREF_SMS_ON", true);
e.commit();

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 to SharedPreferences as the user interacts with them". It gave me the impression that you wanted to change preferences outside the PreferenceActivity (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 add addPreferencesFromResource(R.xml.settings); in your onCreate, for example. But, like I said, your preferences will only change automatically if the user interacts with this activity directly.
I hope that helps.

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