Example usage for android.content SharedPreferences edit

List of usage examples for android.content SharedPreferences edit

Introduction

In this page you can find the example usage for android.content SharedPreferences edit.

Prototype

Editor edit();

Source Link

Document

Create a new Editor for these preferences, through which you can make modifications to the data in the preferences and atomically commit those changes back to the SharedPreferences object.

Usage

From source file:Main.java

public static void setSharedStringPref(Context context, String key, String value) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(key, value);//from w w  w .  ja va  2  s  . com
    editor.apply();
}

From source file:Main.java

static void saveLastPhoneCount(Context context, int count) {
    SharedPreferences prefs = context.getSharedPreferences(AGE_PREFERENCES, Context.MODE_PRIVATE);
    Editor editor = prefs.edit();
    editor.putInt(AGE_LAST_PHONE_COUNT, count);
    editor.commit();//from  w w w .  ja v  a  2 s  .  c o  m
}

From source file:Main.java

public static void clearAllSharedPreferencesList(Context context) {
    SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor sEdit = sPrefs.edit();
    sEdit.clear();/* w w  w  .j  a v a2 s . co  m*/
    sEdit.commit();
}

From source file:Main.java

public static void saveLoginInfo(Context context, String account, String pwd) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = sp.edit();
    editor.putString("account", account);
    editor.putString("password", pwd);
    editor.commit();//from   ww w .j  a v  a  2 s .  c  o  m
}

From source file:Main.java

public static void setBoolPref(Context context, String key, Boolean b) {
    SharedPreferences sharedPref = context.getSharedPreferences(THEME_PREFS, 0);
    SharedPreferences.Editor e = sharedPref.edit();
    e.putBoolean(key, b);/*from w  ww  .jav a 2s  . c  o  m*/
    e.commit();
}

From source file:Main.java

public static void setSharedBooleanPref(Context context, String key, boolean value) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean(key, value);/*from  ww w.  ja  v a2  s  . c o m*/
    editor.apply();
}

From source file:Main.java

public static void save_screen_status(Context context) {
    SharedPreferences localSharedPreferences = context.getSharedPreferences("SCREEN_STATUS", 0);
    Editor localEditor = localSharedPreferences.edit();
    localEditor.putInt("screen_count", localSharedPreferences.getInt("screen_count", 0) + 1);
    localEditor.commit();/*from   www.  j  a v a 2 s. co  m*/
}

From source file:Main.java

private static void writeCache(int[] screen, Activity activity) {

    SharedPreferences config = activity.getSharedPreferences("config", Context.MODE_PRIVATE);
    Editor editor = config.edit();

    editor.putBoolean("isAlreadyGetPhoneResolution", true);
    editor.putInt("height", screen[0]);
    editor.putInt("width", screen[1]);
    editor.commit();//from  w  w w  . java2 s . c om
}

From source file:Main.java

/**
 * Add a {@code (key, value)} data into the given {@code sharedPreferences}.
 * Now, this method supports only {@code boolean} and {@link String}.
 *//*w  w  w.  j ava  2s .  c  o  m*/
public static void updateSharedPreference(SharedPreferences sharedPreferences, String key, Object value) {
    SharedPreferences.Editor editor = sharedPreferences.edit();
    if (value.getClass() == Boolean.class) {
        editor.putBoolean(key, Boolean.class.cast(value));
    } else if (value.getClass() == String.class) {
        editor.putString(key, String.class.cast(value));
    } else if (value.getClass() == Integer.class) {
        editor.putInt(key, Integer.class.cast(value));
    } else {
        throw new IllegalArgumentException("value's type can be only Boolean or String.");
    }
    editor.commit();
}

From source file:Main.java

public static void save(HashMap<String, String> map) {
    SharedPreferences preferences = getPrivateSharedPreference();
    SharedPreferences.Editor editor = preferences.edit();
    for (String key : map.keySet()) {
        editor.putString(key, map.get(key));
    }/*  www.jav a  2 s  . c  o  m*/
    editor.commit();
}