版权声明
- 本文原创作者:谷哥的小弟
- 作者博客地址:http://blog.csdn.net/lfdfhl
SharedPreferencesUtils概述
SharedPreferencesUtils用于在SharedPreferences中进行数据存储操作。
SharedPreferencesUtils截图SharedPreferencesUtils截图如下:
SharedPreferencesUtils源码如下:
import android.content.Context;
import android.content.SharedPreferences;
public class SharedPreferencesUtils {
private static final String PREFERENCE_NAME = "sp_name";
private SharedPreferencesUtils() {}
public static SharedPreferences getSharedPreferences(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
return sharedPreferences;
}
public static SharedPreferences.Editor getEditor(Context context) {
SharedPreferences sharedPreferences = getSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
return editor;
}
public static void putString(Context context, String key, String value) {
SharedPreferences.Editor editor = getEditor(context);
editor.putString(key, value);
editor.apply();
}
public static String getString(Context context, String key) {
return getString(context, key, "");
}
public static String getString(Context context, String key, String defaultValue) {
SharedPreferences sharedPreferences = getSharedPreferences(context);
return sharedPreferences.getString(key, defaultValue);
}
public static void putInt(Context context, String key, int value) {
SharedPreferences.Editor editor = getEditor(context);
editor.putInt(key, value);
editor.apply();
}
public static int getInt(Context context, String key) {
return getInt(context, key, -1);
}
public static int getInt(Context context, String key, int defaultValue) {
SharedPreferences sharedPreferences = getSharedPreferences(context);
return sharedPreferences.getInt(key, defaultValue);
}
public static void putLong(Context context, String key, long value) {
SharedPreferences.Editor editor = getEditor(context);
editor.putLong(key, value);
editor.apply();
}
public static long getLong(Context context, String key) {
return getLong(context, key, -1);
}
public static long getLong(Context context, String key, long defaultValue) {
SharedPreferences sharedPreferences = getSharedPreferences(context);
return sharedPreferences.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 sharedPreferences = getSharedPreferences(context);
return sharedPreferences.getFloat(key, defaultValue);
}
public static boolean putBoolean(Context context, String key, boolean value) {
SharedPreferences.Editor editor = getEditor(context);
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 sharedPreferences = getSharedPreferences(context);
return sharedPreferences.getBoolean(key, defaultValue);
}
public static void clear(Context context) {
SharedPreferences.Editor editor = getEditor(context);
editor.clear();
editor.apply();
}
}



