public static void setDefaults(String key, String value, Context context) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor editor = prefs.edit(); editor.putString(key, value); editor.commit(); } public static String getDefaults(String key, Context context) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); return preferences.getString(key, null); }
private SharedPreferences mPreferences; .... boolean firstTime = mPreferences.getBoolean("firstTime", true); if (firstTime) { SharedPreferences.Editor editor = mPreferences.edit(); editor.putBoolean("firstTime", false); editor.commit(); showMiddleActivity(); }
@Test public void commit_shouldRemoveValuesThenSetValues() throws Exception { editor.putString("deleteMe", "foo").commit(); editor.remove("deleteMe"); editor.putString("dontDeleteMe", "baz"); editor.remove("dontDeleteMe"); editor.commit(); SharedPreferences anotherSharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE); assertThat(anotherSharedPreferences.getBoolean("boolean", false)).isTrue(); assertThat(anotherSharedPreferences.getFloat("float", 666f)).isEqualTo(1.1f); assertThat(anotherSharedPreferences.getInt("int", 666)).isEqualTo(2); assertThat(anotherSharedPreferences.getLong("long", 666L)).isEqualTo(3L); assertThat(anotherSharedPreferences.getString("string", "wacka wa")).isEqualTo("foobar"); assertThat(anotherSharedPreferences.getString("deleteMe", "awol")).isEqualTo("awol"); assertThat(anotherSharedPreferences.getString("dontDeleteMe", "oops")).isEqualTo("oops"); }
public SharedPreferencesUtil putBoolean(String key, boolean value) { editor.putBoolean(key, value); editor.commit(); return this; }
public void setFirstLaunch(boolean firstLaunch) { prefsEditor.putBoolean(FIRST_LAUNCH, firstLaunch); prefsEditor.commit(); }
public void setActivateVideoMonitoring(boolean active) { prefsEditor.putBoolean(context.getResources().getString(R.string.video_active_preference_key), active); prefsEditor.commit(); }
public void activateFlash(boolean active) { prefsEditor.putBoolean(FLASH_ACTIVE, active); prefsEditor.commit(); }
public void activateMicrophone(boolean active) { prefsEditor.putBoolean(MICROPHONE_ACTIVE, active); prefsEditor.commit(); }
private static final String MAP = "map"; private static final Type MAP_TYPE = new TypeToken<Map<MyObjA, MyObjB>>() {}.getType(); private static SharedPreferences prefs = MyApplication.getContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); private static Map<MyObjA, MyObjB> myMap; public static void saveMap (Map<MyObjA, MyObjB> map) { SharedPreferences.Editor editor = prefs.edit(); editor.putString(MAP, new Gson().toJson(map)); editor.commit(); myMap = map; } public static Map<MyObjA, MyObjB> loadMap() { if (myMap == null) { myMap = new Gson().fromJson(prefs.getString(MAP, null), MAP_TYPE); } return myMap; }
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); boolean previouslyStarted = prefs.getBoolean(getString(R.string.pref_previously_started), false); if(!previouslyStarted) { SharedPreferences.Editor edit = prefs.edit(); edit.putBoolean(getString(R.string.pref_previously_started), Boolean.TRUE); edit.commit(); showHelp(); }
@Test public void commit_shouldClearThenSetValues() throws Exception { editor.putString("deleteMe", "foo"); editor.clear(); editor.putString("dontDeleteMe", "baz"); editor.commit(); SharedPreferences anotherSharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE); assertTrue(anotherSharedPreferences.getBoolean("boolean", false)); assertThat(anotherSharedPreferences.getFloat("float", 666f)).isEqualTo(1.1f); assertThat(anotherSharedPreferences.getInt("int", 666)).isEqualTo(2); assertThat(anotherSharedPreferences.getLong("long", 666L)).isEqualTo(3L); assertThat(anotherSharedPreferences.getString("string", "wacka wa")).isEqualTo("foobar"); // Android always calls clear before put on any open editor, so here "foo" is preserved rather than cleared. assertThat(anotherSharedPreferences.getString("deleteMe", "awol")).isEqualTo("foo"); assertThat(anotherSharedPreferences.getString("dontDeleteMe", "oops")).isEqualTo("baz"); }