Refine search
/** * 返回存在sharedPreferences的信息 * * @param key * @return */ public static String getStringSF(Context context, String key) { if (mSharedPreferences == null) { mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE); } return mSharedPreferences.getString(key, null); }
/** * you can set Tinker disable in runtime at some times! * * @param context */ public static void setTinkerDisableWithSharedPreferences(Context context) { SharedPreferences sp = context.getSharedPreferences(ShareConstants.TINKER_SHARE_PREFERENCE_CONFIG, Context.MODE_MULTI_PROCESS); String keyName = ShareConstants.TINKER_ENABLE_CONFIG_PREFIX + ShareConstants.TINKER_VERSION; sp.edit().putBoolean(keyName, false).commit(); }
private static String uniqueID = null; private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID"; public synchronized static String id(Context context) { if (uniqueID == null) { SharedPreferences sharedPrefs = context.getSharedPreferences( PREF_UNIQUE_ID, Context.MODE_PRIVATE); uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null); if (uniqueID == null) { uniqueID = UUID.randomUUID().toString(); Editor editor = sharedPrefs.edit(); editor.putString(PREF_UNIQUE_ID, uniqueID); editor.commit(); } } return uniqueID; }
@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 PreferenceManager(Context context) { this.context = context; this.appSharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, AppCompatActivity.MODE_PRIVATE); this.prefsEditor = appSharedPrefs.edit(); }
/** * @param paramList * @return */ public String getToken(String tag) { return context.getSharedPreferences(KEY_TOKEN, Context.MODE_PRIVATE).getString(KEY_TOKEN + tag, ""); } /**
public class AppPreferences { public static final String KEY_PREFS_SMS_BODY = "sms_body"; private static final String APP_SHARED_PREFS = AppPreferences.class.getSimpleName(); // Name of the file -.xml private SharedPreferences _sharedPrefs; private Editor _prefsEditor; public AppPreferences(Context context) { this._sharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE); this._prefsEditor = _sharedPrefs.edit(); } public String getSmsBody() { return _sharedPrefs.getString(KEY_PREFS_SMS_BODY, ""); } public void saveSmsBody(String text) { _prefsEditor.putString(KEY_PREFS_SMS_BODY, text); _prefsEditor.commit(); } }
private SPUtils(Context context) { this.context = context; sp = this.context.getSharedPreferences(SP_NAME, Context.MODE_APPEND); editor = sp.edit(); }
/** * @param paramList * @must demo_***改为服务器设定值 * @return */ public String getToken(String tag) { return context.getSharedPreferences(KEY_TOKEN, Context.MODE_PRIVATE).getString(KEY_TOKEN + tag, ""); } /**
public static void clean(Context context) { SharedPreferences sp = context.getSharedPreferences(PREFS, 0); File file = new File(context.getExternalCacheDir(), sp.getString(KEY_UPDATE, "") + ".apk"); UpdateUtil.log("apk ==> " + file.toString()); if (file.exists()) { file.delete(); } sp.edit().clear().apply(); }
/** * 存储重要信息到sharedPreferences; * * @param key * @param value */ public static void setStringSF(Context context, String key, String value) { if (mSharedPreferences == null) { mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE); } mSharedPreferences.edit().putString(key, value).apply(); }
/** * @return */ public String getCookie() { return context.getSharedPreferences(KEY_COOKIE, Context.MODE_PRIVATE).getString(KEY_COOKIE, ""); } /**
@Test public void commit_shouldClearEditsThatNeedRemoveAndEditsThatNeedCommit() throws Exception { editor.commit(); editor.remove("string").commit(); assertThat(sharedPreferences.getString("string", "no value for key")).isEqualTo("no value for key"); SharedPreferences anotherSharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE); anotherSharedPreferences.edit().putString("string", "value for key").commit(); editor.commit(); assertThat(sharedPreferences.getString("string", "no value for key")).isEqualTo("value for key"); }
/** * 清除某个内容 */ public static void removeSF(Context context, String key) { if (mSharedPreferences == null) { mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE); } mSharedPreferences.edit().remove(key).apply(); }
/** * @return */ public String getCookie() { return context.getSharedPreferences(KEY_COOKIE, Context.MODE_PRIVATE).getString(KEY_COOKIE, ""); } /**
/** * 存储重要信息到sharedPreferences; * * @param key * @param value */ public static void setIntergerSF(Context context, String key, int value) { if (mSharedPreferences == null) { mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE); } mSharedPreferences.edit().putInt(key, value).apply(); }
/** * @param tag * @must demo_***改为服务器设定值 * @return */ public String getToken(String tag) { return context.getSharedPreferences(KEY_TOKEN, Context.MODE_PRIVATE).getString(KEY_TOKEN + tag, ""); } /**
private SharedPreferencesHelper(Context context) { sp = context.getSharedPreferences(SHARED_PATH, Context.MODE_PRIVATE); editor = sp.edit(); }
/** * @return */ public String getCookie() { return context.getSharedPreferences(KEY_COOKIE, Context.MODE_PRIVATE).getString(KEY_COOKIE, ""); } /**
/** * 清除Shareprefrence */ public static void clearShareprefrence(Context context) { if (mSharedPreferences == null) { mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE); } mSharedPreferences.edit().clear().apply(); }