/**
* 覆盖式存储
*
* @param mContext
* @param key
* @param content
*/
public static void save(Context mContext, String key, String content) {
SharedPreferences mSharedPreferences = mContext.getSharedPreferences("save", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mSharedPreferences.edit();//SharedPreferences编辑的Editor对象
editor.putString(key, content);
editor.commit();//提交
}
/**
* 取
*
* @param mContext
* @param key
* @return
*/
public static String get(Context mContext, String key) {
SharedPreferences mSharedPreferences = mContext.getSharedPreferences("save", Context.MODE_PRIVATE);
String value = mSharedPreferences.getString(key, "");
return value;
}
/***
* 封装通过sharedpreferences存值
* @param context
* @param key
* @param object
*/
public static void putInfo(Context context, String key, Object object) {
SharedPreferences sp = context.getSharedPreferences("save", 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 Boolean) {
editor.putBoolean(key, (Boolean) object);
} else if (object instanceof Long) {
editor.putLong(key, (Long) object);
} else {
editor.putString(key, object.toString());
}
editor.commit();
}
/****
*
* 封装通过sharedpreferences取值
* @param context
* @param key
* @param defaultObject
* @return
*/
public static Object getInfo(Context context, String key, Object defaultObject) {
SharedPreferences sp = context.getSharedPreferences("save", Context.MODE_PRIVATE);
if (defaultObject instanceof String) {
return sp.getString(key, (String) defaultObject);
} else if (defaultObject instanceof Integer) {
return sp.getInt(key, (Integer) defaultObject);
} else if (defaultObject instanceof Boolean) {
return sp.getBoolean(key, (Boolean) defaultObject);
} else if (defaultObject instanceof Float) {
return sp.getFloat(key, (Float) defaultObject);
} else if (defaultObject instanceof Long) {
return sp.getLong(key, (Long) defaultObject);
}
return null;
}