/** * 保存数据的方法,根据类型调用不同的保存方法 */ public static void put(Context context,String key,Object object){ SharedPreferences sp = context.getSharedPreferences(getSpName(context),Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); if (object instanceof String){ editor.putString(key, (String) object); }else if (object instanceof Integer){ editor.putInt(key, (Integer) object); }else if (object instanceof Float){ editor.putFloat(key, (Float) object); }else if (object instanceof Long){ editor.putLong(key, (Long) object); }else if (object instanceof Boolean){ editor.putBoolean(key, (Boolean) object); }else { editor.putString(key,object.toString()); } //异步提交 org.androidannotations.api.sharedpreferences.SharedPreferencesCompat.apply(editor); }
private void saveXmlData() { if (isUseXmlCache()) { final Context context = mView.getContext(); final String content = mView.getContent(); final String[] paths = mView.getImages(); SharedPreferences sharedPreferences = context.getSharedPreferences(SHARE_FILE_NAME, Activity.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(SHARE_VALUES_CONTENT, content); if (paths != null && paths.length > 0) { editor.putStringSet(SHARE_VALUES_IMAGES, CollectionUtil.toHashSet(paths)); } else { editor.putStringSet(SHARE_VALUES_IMAGES, null); } SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); } }
/** * 存入某个key对应的value值 * * @param context * @param key * @param value */ public static void put(Context context, String key, Object value) { SharedPreferences sp = context.getSharedPreferences(FILL_NAME, Context.MODE_PRIVATE); Editor edit = sp.edit(); if (value instanceof String) { edit.putString(key, (String) value); } else if (value instanceof Integer) { edit.putInt(key, (Integer) value); } else if (value instanceof Boolean) { edit.putBoolean(key, (Boolean) value); } else if (value instanceof Float) { edit.putFloat(key, (Float) value); } else if (value instanceof Long) { edit.putLong(key, (Long) value); } else if (value instanceof Set) { edit.putStringSet(key, (Set<String>) value); } SharedPreferencesCompat.EditorCompat.getInstance().apply(edit); }
public static void putVerse(@NonNull Context context, @NonNull Votd verse) { if (!TextUtils.isEmpty(verse.text) && !TextUtils.isEmpty(verse.displayRef) && !TextUtils.isEmpty(verse.year) && !TextUtils.isEmpty(verse.month) && !TextUtils.isEmpty(verse.day) && !TextUtils.isEmpty(verse.version)) { // getting the current language configuration final String language = ResourcesUtil.getCurrentLanguage(context); final SharedPreferences preferences = getDefaultSharedPreferences(context); final SharedPreferences.Editor editor = preferences.edit(); editor.putString(PREF_VERSE, verse.text); editor.putString(PREF_REFERENCE, verse.displayRef); editor.putString(PREF_YEAR_OF_VERSE, verse.year); editor.putString(PREF_MONTH_OF_VERSE, verse.month); editor.putString(PREF_DAY_OF_VERSE, verse.day); editor.putString(PREF_LANGUAGE, language); editor.putString(PREF_VERSION, verse.version); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); } }
/** * 移除Key对应的value值 */ public static void remove(Context context,String key){ SharedPreferences sp = context.getSharedPreferences(getSpName(context),Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.remove(key); org.androidannotations.api.sharedpreferences.SharedPreferencesCompat.apply(editor); }
/** * 清除所有数据 */ public static void clear(Context context){ SharedPreferences sp = context.getSharedPreferences(getSpName(context),Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.clear(); org.androidannotations.api.sharedpreferences.SharedPreferencesCompat.apply(editor); }
private void clearAndFinish(Context context) { if (isUseXmlCache()) { SharedPreferences sharedPreferences = context.getSharedPreferences(SHARE_FILE_NAME, Activity.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(SHARE_VALUES_CONTENT, null); editor.putStringSet(SHARE_VALUES_IMAGES, null); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); } mView.finish(); }
/** * hold account information */ private void holdAccount() { String username = mEtLoginUsername.getText().toString().trim(); if (!TextUtils.isEmpty(username)) { SharedPreferences sp = getSharedPreferences(UserConstants.HOLD_ACCOUNT, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putString(HOLD_USERNAME_KEY, username); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); } }
private static String getAppId(Application context) { if (context != null) { SharedPreferences sp = Setting.getSettingPreferences(context); String uniqueID = sp.getString(Setting.KEY_APP_UNIQUE_ID, null); if (TextUtils.isEmpty(uniqueID)) { uniqueID = UUID.randomUUID().toString(); SharedPreferences.Editor editor = sp.edit().putString(Setting.KEY_APP_UNIQUE_ID, uniqueID); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); } return uniqueID; } return UUID.randomUUID().toString(); }
/** * 移除某个key值已经对应的值 * * @param context * @param key */ public static void remove(Context context, String key) { SharedPreferences sp = context.getSharedPreferences(FILL_NAME, Context.MODE_PRIVATE); Editor edit = sp.edit(); edit.remove(key); SharedPreferencesCompat.EditorCompat.getInstance().apply(edit); }
/** * 清除所有内容 * * @param context */ public static void clear(Context context) { SharedPreferences sp = context.getSharedPreferences(FILL_NAME, Context.MODE_PRIVATE); Editor edit = sp.edit(); edit.clear(); SharedPreferencesCompat.EditorCompat.getInstance().apply(edit); }
@Override public void saveActivityInfo(@NonNull ActivityInfo activityInfo) { checkNotNull(activityInfo); String mimeType = checkNotNull(activityInfo.getMimeType()); String pkg = checkNotNull(activityInfo.getPkg()); String cls = checkNotNull(activityInfo.getCls()); SharedPreferences.Editor editor = mPreferences.edit(); editor.putString(mimeType, String.format("%s|%s", pkg, cls)); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); }
@Override public int deleteActivityInfo(@Nullable String mimeType) { if (mimeType == null) { return 0; } else { if (mPreferences.contains(mimeType)) { SharedPreferences.Editor editor = mPreferences.edit(); editor.remove(mimeType); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); return 1; } else { return 0; } } }
@Override public int deleteAllActivityInfos() { Map<String, ?> map = mPreferences.getAll(); if (map == null || map.size() == 0) { return 0; } else { SharedPreferences.Editor editor = mPreferences.edit(); editor.clear(); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); return map.size(); } }
@Override protected void onStop() { super.onStop(); if(!TextUtils.isEmpty(mUsername.getText())){ SharedPreferencesCompat.EditorCompat.getInstance().apply( PreferenceManager.getDefaultSharedPreferences(this).edit().putString("last_username", String.valueOf(mUsername.getText()))); } }
private void setNoCommitFix(boolean noCommit) throws IllegalAccessException { SharedPreferences.Editor editor = (SharedPreferences.Editor) editorField.get(this); if (!noCommit && editor != null) { SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); } this.noCommit = noCommit; }
@Override public void onSkinChange(int mode, Activity activity) { SharedPreferences preferences = activity.getSharedPreferences("NightOwlDemo", Activity.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putInt("mode", mode); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); }
public static void set(String key, int value) { Editor editor = getPreferences().edit(); editor.putInt(key, value); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); }
public static void set(String key, boolean value) { Editor editor = getPreferences().edit(); editor.putBoolean(key, value); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); }
public static void set(String key, String value) { Editor editor = getPreferences().edit(); editor.putString(key, value); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); }
private static int updateSaveVersionCode(Context context, int version) { SharedPreferences sp = getSettingPreferences(context); SharedPreferences.Editor editor = sp.edit().putInt(KEY_VERSION_CODE, version); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); return version; }
public static void updateServerUrl(Context context, String url) { SharedPreferences sp = getSettingPreferences(context); SharedPreferences.Editor editor = sp.edit().putString(KEY_SEVER_URL, url); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); }
public static void updateSystemConfigTimeStamp(Context context) { SharedPreferences sp = getSettingPreferences(context); SharedPreferences.Editor editor = sp.edit().putLong(KEY_SYSTEM_CONFIG_TIMESTAMP, System.currentTimeMillis()); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); }
public static void updateLocationInfo(Context context, boolean hasLocation) { SharedPreferences sp = getSettingPreferences(context); SharedPreferences.Editor editor = sp.edit().putBoolean(KEY_LOCATION_INFO, hasLocation); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); }
public static void updateLocationPermission(Context context, boolean hasPermission) { SharedPreferences sp = getSettingPreferences(context); SharedPreferences.Editor editor = sp.edit().putBoolean(KEY_LOCATION_PERMISSION, hasPermission); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); }
public static void updateLocationAppCode(Context context, int appCode) { SharedPreferences sp = getSettingPreferences(context); SharedPreferences.Editor editor = sp.edit().putInt(KEY_LOCATION_APP_CODE, appCode); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); }
public static void updateSoftKeyboardHeight(Context context, int height) { SharedPreferences sp = getSettingPreferences(context); SharedPreferences.Editor editor = sp.edit().putInt(KEY_SOFT_KEYBOARD_HEIGHT, height); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); }
/** * 保存一个Bean到{@link SharedPreferences}文件中, * 该方法会调用{@link #getSharedPreferences(Context, Class)}方法, * 创建SharedPreferences文件,如果文件不存在会进行新建操作 * * @param context Context * @param t Bean * @param <T> Any Bean * @return 返回是否保存成功 */ public static <T> boolean save(Context context, T t) { final Class<?> clx = t.getClass(); // We should remove all data before save data remove(context, clx); // Get all data form t Map<String, Data> map = new ArrayMap<>(); buildValuesToMap(clx, t, "", map); SharedPreferences sp = getSharedPreferences(context, clx); SharedPreferences.Editor editor = sp.edit(); // Get all existing key Set<String> existKeys = sp.getAll().keySet(); // Foreach the sava data Set<String> keys = map.keySet(); for (String key : keys) { Data data = map.get(key); final Class<?> type = data.type; final Object value = data.value; try { if (value == null) { removeKeyFamily(editor, existKeys, key); } else if (type.equals(Byte.class) || type.equals(byte.class)) { editor.putInt(key, (Byte) value); } else if (type.equals(Short.class) || type.equals(short.class)) { editor.putInt(key, (Short) value); } else if (type.equals(Integer.class) || type.equals(int.class)) { editor.putInt(key, (Integer) value); } else if (type.equals(Long.class) || type.equals(long.class)) { editor.putLong(key, (Long) value); } else if (type.equals(Float.class) || type.equals(float.class)) { editor.putFloat(key, (Float) value); } else if (type.equals(Double.class) || type.equals(double.class)) { editor.putString(key, (String.valueOf(value))); } else if (type.equals(Boolean.class) || type.equals(boolean.class)) { editor.putBoolean(key, (Boolean) value); } else if (type.equals(Character.class) || type.equals(char.class)) { editor.putString(key, value.toString()); } else if (type.equals(String.class)) { editor.putString(key, value.toString()); } else { Logger.d(TAG, String.format("Con't support save this type:%s, value:%s, key:%s", type, value, key)); } } catch (IllegalArgumentException e) { Logger.d(TAG, "Save error:" + e.getMessage()); } } SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); return true; }
private void tryCommit(@NonNull SharedPreferences.Editor editor) { if (mPreferenceManager.shouldCommit()) { SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); } }
/** * Similar to {@link #setDefaultValues(Context, int, boolean)} but allows * the client to provide the filename and mode of the shared preferences * file. * * @param context The context of the shared preferences. * @param sharedPreferencesName A custom name for the shared preferences file. * @param sharedPreferencesMode The file creation mode for the shared preferences file, such * as {@link android.content.Context#MODE_PRIVATE} or {@link * android.content.Context#MODE_PRIVATE} * @param resId The resource ID of the preference XML file. * @param readAgain Whether to re-read the default values. * If false, this method will set the default values only if this * method has never been called in the past (or if the * {@link #KEY_HAS_SET_DEFAULT_VALUES} in the default value shared * preferences file is false). To attempt to set the default values again * bypassing this check, set {@code readAgain} to true. * <p class="note"> * Note: this will NOT reset preferences back to their default * values. For that functionality, use * {@link PreferenceManager#getDefaultSharedPreferences(Context)} * and clear it followed by a call to this method with this * parameter set to true. * * @see #setDefaultValues(Context, int, boolean) * @see #setSharedPreferencesName(String) * @see #setSharedPreferencesMode(int) */ public static void setDefaultValues(Context context, String sharedPreferencesName, int sharedPreferencesMode, int resId, boolean readAgain) { final SharedPreferences defaultValueSp = context.getSharedPreferences( KEY_HAS_SET_DEFAULT_VALUES, Context.MODE_PRIVATE); if (readAgain || !defaultValueSp.getBoolean(KEY_HAS_SET_DEFAULT_VALUES, false)) { final PreferenceManager pm = new PreferenceManager(context); pm.setSharedPreferencesName(sharedPreferencesName); pm.setSharedPreferencesMode(sharedPreferencesMode); pm.inflateFromResource(context, resId, null); SharedPreferences.Editor editor = defaultValueSp.edit().putBoolean(KEY_HAS_SET_DEFAULT_VALUES, true); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); } }
private void setNoCommit(boolean noCommit) { if (!noCommit && mEditor != null) { SharedPreferencesCompat.EditorCompat.getInstance().apply(mEditor); } mNoCommit = noCommit; }
public static void markWelcomeDone(final Context context) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferencesCompat.EditorCompat.getInstance() .apply(sp.edit().putBoolean(PREF_WELCOME_DONE, true)); }
public static void remove(@NonNull final String key) { SharedPreferencesCompat.EditorCompat.getInstance().apply(get(null).edit().remove(key)); }
public static void remove(@NonNull final String name, @NonNull final String key) { SharedPreferencesCompat.EditorCompat.getInstance().apply(get(name).edit().remove(key)); }
public static void clear() { SharedPreferencesCompat.EditorCompat.getInstance().apply(get(null).edit().clear()); }
public static void clear(@NonNull final String name) { SharedPreferencesCompat.EditorCompat.getInstance().apply(get(name).edit().clear()); }
public static void putString(@NonNull final String key, final String value) { LogHelper.verbose(key + " = " + value); SharedPreferencesCompat.EditorCompat.getInstance().apply(get(null).edit().putString(key, value)); }
public static void putString(@NonNull final String name, @NonNull final String key, final String value) { LogHelper.verbose(name + ": " + key + " = " + value); SharedPreferencesCompat.EditorCompat.getInstance().apply(get(name).edit().putString(key, value)); }
public static void putStringSet(@NonNull final String key, final Set<String> value) { LogHelper.verbose(key + " = " + value); SharedPreferencesCompat.EditorCompat.getInstance().apply(get(null).edit().putStringSet(key, value)); }
public static void putStringSet(@NonNull final String name, @NonNull final String key, final Set<String> value) { LogHelper.verbose(name + ": " + key + " = " + value); SharedPreferencesCompat.EditorCompat.getInstance().apply(get(name).edit().putStringSet(key, value)); }