安卓中用到的PreferencesUtils 類
阿新 • • 發佈:2018-12-04
public class PreferencesUtils { public static String PREFERENCE_NAME = "XXX"; public static boolean putString(Context context, String key, String value) { SharedPreferences.Editor editor = getEditor(context); editor.putString(key, value); return editor.commit();} private static SharedPreferences.Editor getEditor(Context context) { SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); return settings.edit(); } public static String getString(Context context, String key) { returngetString(context, key, null); } public static String getString(Context context, String key, String defaultValue) { SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); return settings.getString(key, defaultValue);} public static boolean putInt(Context context, String key, int value) { SharedPreferences.Editor editor = getEditor(context); editor.putInt(key, value); return editor.commit(); } public static int getInt(Context context, String key) { return getInt(context, key, -1); } public static int getInt(Context context, String key, int defaultValue) { SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); return settings.getInt(key, defaultValue); } public static boolean putLong(Context context, String key, long value) { SharedPreferences.Editor editor = getEditor(context); editor.putLong(key, value); return editor.commit(); } public static long getLong(Context context, String key) { return getLong(context, key, -1); } public static long getLong(Context context, String key, long defaultValue) { SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); return settings.getLong(key, defaultValue); } public static boolean putFloat(Context context, String key, float value) { SharedPreferences.Editor editor = getEditor(context); editor.putFloat(key, value); return editor.commit(); } public static float getFloat(Context context, String key) { return getFloat(context, key, -1); } public static float getFloat(Context context, String key, float defaultValue) { SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); return settings.getFloat(key, defaultValue); } public static boolean putBoolean(Context context, String key, boolean value) { SharedPreferences.Editor editor = getEditor(context); editor.putBoolean(key, value); return editor.commit(); } public static boolean getBoolean(Context context, String key) { return getBoolean(context, key, false); } public static boolean getBoolean(Context context, String key, boolean defaultValue) { SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); return settings.getBoolean(key, defaultValue); } }