/**
* @anthor GrainRain
* @funcation SharedPreference工具类
* @date 2019/9/5
*/
public class SharedPreferenceUtils {
private static SharedPreferences preferences;
private static SharedPreferences.Editor editor;
private static final String TAG = "SharedPreference";
/**
* 储存数据
* @param key
* @param object
*/
public static void put(Context context, String key, Object object) {
preferences = PreferenceManager.getDefaultSharedPreferences(context);
editor = preferences.edit();
if(editor != null) {
if (object instanceof String) {
editor.putString(key, (String) object);
} else if (object instanceof Integer) {
editor.putInt(key, (Integer) object);
} else if (object instanceof Boolean) {
editor.putBoolean(key, (Boolean) object);
} else if (object instanceof Float) {
editor.putFloat(key, (Float) object);
} else if (object instanceof Long) {
editor.putLong(key, (Long) object);
} else {
editor.putString(key, object.toString());
}
editor.commit();
} else {
Log.e(TAG, "请重试");
}
}
/**
* 获取数据
* @param key
* @param defaultObject
* @return Object
*/
public static Object get(Context context, String key, Object defaultObject) {
preferences = PreferenceManager.getDefaultSharedPreferences(context);
editor = preferences.edit();
if(preferences != null) {
if (defaultObject instanceof String) {
return preferences.getString(key, (String) defaultObject);
} else if (defaultObject instanceof Integer) {
return preferences.getInt(key, (Integer) defaultObject);
} else if (defaultObject instanceof Boolean) {
return preferences.getBoolean(key, (Boolean) defaultObject);
} else if (defaultObject instanceof Float) {
return preferences.getFloat(key, (Float) defaultObject);
} else if (defaultObject instanceof Long) {
return preferences.getLong(key, (Long) defaultObject);
} else {
return preferences.getString(key, null);
}
} else {
Log.e(TAG, "请重试");
return null;
}
}
/**
* 移除key对应的值
* @param key
*/
public static void remove(Context context, String key) {
preferences = PreferenceManager.getDefaultSharedPreferences(context);
editor = preferences.edit();
if(editor != null) {
editor.remove(key);
editor.commit();
} else {
Log.e(TAG, "请重试");
}
}
/**
* 清除所有数据
*/
public static void clear(Context context) {
preferences = PreferenceManager.getDefaultSharedPreferences(context);
editor = preferences.edit();
if(editor != null) {
editor.clear();
editor.commit();
} else {
Log.e(TAG, "请重试");
}
}
/**
* 查询key是否存在
*/
public static Boolean contain(Context context, String key) {
preferences = PreferenceManager.getDefaultSharedPreferences(context);
editor = preferences.edit();
if(preferences != null) {
return preferences.contains(key);
} else {
Log.e(TAG, "请重试");
return false;
}
}
/**
* 返回所有键值对
* @return
*/
public static Map<String, ?> getAll(Context context) {
preferences = PreferenceManager.getDefaultSharedPreferences(context);
editor = preferences.edit();
if(preferences != null) {
return preferences.getAll();
} else {
Log.e(TAG, "请重试");
return null;
}
}
}
Android 封装SharedPreference工具类
最新推荐文章于 2024-09-05 00:04:53 发布