Search code examples
androiddefaultsharedpreference

How to set a DefaultSharedPreferences value?


I have a preferences activity with a checkbox "Enable Service".

I read the value like this :

SharedPreferences prefs = 
PreferenceManager.getDefaultSharedPreferences(con);
ServiceEnabled_Pref = prefs.getBoolean("EnableService", true);

ok, but how can I set this Preference ?

this is not a CustomShared Preference, It is a DefaultShared Preferences and it seems that there is not a method putBoolean for DefaultSharedPreferences.

I need this, cause I have a Widget with a button that needs to set this value to true/false


Solution

  • programatically:

    prefs.editor().putBoolean("EnableService", true).commit();
    

    however your best bet for defining the preferenceActivity and its defaults is via an xml file as decribed here with the android:defaultValue attribute