final String PREFS_NAME = "MyPrefsFile"; SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); if (settings.getBoolean("my_first_time", true)) { //the app is being launched for first time, do something Log.d("Comments", "First time"); // first time task // record the fact that the app has been started at least once settings.edit().putBoolean("my_first_time", false).commit(); }
@Test public void commit_shouldStoreValues() throws Exception { 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"); assertThat(anotherSharedPreferences.getStringSet("stringSet", null)).isEqualTo(stringSet); }
public class MyActivity extends Activity { SharedPreferences prefs = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Perhaps set content view here prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE); } @Override protected void onResume() { super.onResume(); if (prefs.getBoolean("firstrun", true)) { // Do first run stuff here then set 'firstrun' as false // using the following line to edit/commit prefs prefs.edit().putBoolean("firstrun", false).commit(); } } }
@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"); }
SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this); boolean firstRun = p.getBoolean(PREFERENCE_FIRST_RUN, true); p.edit().putBoolean(PREFERENCE_FIRST_RUN, false).commit();
@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"); }
private SharedPreferences mPreferences; .... boolean firstTime = mPreferences.getBoolean("firstTime", true); if (firstTime) { SharedPreferences.Editor editor = mPreferences.edit(); editor.putBoolean("firstTime", false); editor.commit(); showMiddleActivity(); }
monstart = pref.getBoolean("monstart", monstart); enable_monMode = pref.getString("enable_monMode", enable_monMode); disable_monMode = pref.getString("disable_monMode", disable_monMode); enable_on_airodump = pref.getBoolean("enable_on_airodump", enable_on_airodump); show_notif = pref.getBoolean("show_notif", show_notif); show_details = pref.getBoolean("show_details", show_details); airOnStartup = pref.getBoolean("airOnStartup", airOnStartup); debug = pref.getBoolean("debug", debug); watchdog = pref.getBoolean("watchdog", watchdog); target_deauth = pref.getBoolean("target_deauth", target_deauth); delete_extra = pref.getBoolean("delete_extra", delete_extra); try{ always_cap = pref.getBoolean("always_cap", always_cap); }catch(ClassCastException e){ pref_edit.putBoolean("always_cap", false); pref_edit.commit(); cont_on_fail = pref.getBoolean("cont_on_fail", cont_on_fail); update_on_startup = pref.getBoolean("update_on_startup", update_on_startup); band = Integer.parseInt(pref.getString("band", Integer.toString(band))); show_client_count = pref.getBoolean("show_client_count", show_client_count);
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(); }
SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(this); boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true); if (isFirstRun) { // Code to run once SharedPreferences.Editor editor = wmbPreference.edit(); editor.putBoolean("FIRSTRUN", false); editor.commit(); }
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); if(!prefs.getBoolean("firstTime", false)) { // run your one time code SharedPreferences.Editor editor = prefs.edit(); editor.putBoolean("firstTime", true); editor.commit(); }
SharedPreferences runCheck = PreferenceManager.getSharedPreferences("hasRunBefore", 0); //load the preferences Boolean hasRun = runCheck.getBoolean("hasRun", false); //see if it's run before, default no if (!hasRun) { SharedPreferences settings = getSharedPreferences("hasRunBefore", 0); SharedPreferences.Editor edit = settings.edit(); edit.putBoolean("hasRun", true); //set to has run edit.commit(); //apply //code for if this is the first time the app has run } else { //code if the app HAS run before }
public void onCreate(Bundle savedInstanceState) { // don't forget to call super method. super.onCreate(savedInstanceState); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); if (!prefs.getBoolean("firstTime", false)) { // <---- run your one time code here databaseSetup(); // mark first time has runned. SharedPreferences.Editor editor = prefs.edit(); editor.putBoolean("firstTime", true); editor.commit(); } }
public class App extends Application { @Override public void onCreate() { super.onCreate(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); if (!prefs.getBoolean("firstTime", false)) { // <---- run your one time code here databaseSetup(); // mark first time has runned. SharedPreferences.Editor editor = prefs.edit(); editor.putBoolean("firstTime", true); editor.commit(); } }
public class MyActivity extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; @Override protected void onCreate(Bundle state){ super.onCreate(state); SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); boolean dialogShown = settings.getBoolean("dialogShown", false); if (!dialogShown) { // AlertDialog code here SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("dialogShown", true); editor.commit(); } }
void setTorch(Camera camera, boolean newSetting) { Camera.Parameters parameters = camera.getParameters(); doSetTorch(parameters, newSetting); camera.setParameters(parameters); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean currentSetting = prefs.getBoolean(PreferencesActivity.KEY_TOGGLE_LIGHT, false); if (currentSetting != newSetting) { SharedPreferences.Editor editor = prefs.edit(); editor.putBoolean(PreferencesActivity.KEY_TOGGLE_LIGHT, newSetting); editor.commit(); } }
public void show(String tagKey, OnceCallback callback) { boolean isSecondTime = mSharedPreferences.getBoolean(tagKey, false); if (!isSecondTime) { callback.onOnce(); SharedPreferences.Editor editor = mSharedPreferences.edit(); editor.putBoolean(tagKey, true); editor.apply(); } }
Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false); SharedPreferences.Editor editor = mPrefs.edit(); editor.putBoolean(welcomeScreenShownPref, true); editor.commit(); // Very important to save the preference
public void updateToCurrentVersionIfNecessary() { // Check prefs to see if we have converted from legacy (pre 4.8.0) ACRA if (!prefs.getBoolean(PREF__LEGACY_ALREADY_CONVERTED_TO_4_8_0, false)) { // If not then move reports to approved/unapproved folders and mark as converted. new ReportMigrator(context).migrate(); // Mark as converted. prefs.edit().putBoolean(PREF__LEGACY_ALREADY_CONVERTED_TO_4_8_0, true).apply(); } if (!prefs.getBoolean(PREF__LEGACY_ALREADY_CONVERTED_TO_JSON, false)) { new ReportConverter(context).convert(); // Mark as converted. prefs.edit().putBoolean(PREF__LEGACY_ALREADY_CONVERTED_TO_JSON, true).apply(); } } }
// parse Preference file SharedPreferences preferences = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE); // get values from Map preferences.getBoolean("key", defaultValue) preferences.get..("key", defaultValue) // you can get all Map but be careful you must not modify the collection returned by this // method, or alter any of its contents. Map<String, ?> all = preferences.getAll(); // get Editor object SharedPreferences.Editor editor = preferences.edit(); //add on Change Listener preferences.registerOnSharedPreferenceChangeListener(mListener); //remove on Change Listener preferences.unregisterOnSharedPreferenceChangeListener(mListener); // listener example SharedPreferences.OnSharedPreferenceChangeListener mOnSharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { } };