Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top