创建使用工具类:
/**
*
* 简单信息保存工具类
*/
public class SpUtils {
public static void putBoolean(Context ctx, String key, boolean value) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
sp.edit().putBoolean(key, value).commit();
}
public static void putStringSet(Context ctx, String key, Set<String> value) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
sp.edit().putStringSet(key, value).commit();
}
public static Set<String> getStringSet(Context ctx, String key, Set<String> defValue) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
return sp.getStringSet(key, defValue);
}
public static boolean getBoolean(Context ctx, String key, boolean defValue) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
return sp.getBoolean(key, defValue);
}
public static void putString(Context ctx, String key, String value) {
if (null == ctx) {
return;
}
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
sp.edit().putString(key, value).commit();
}
public static String getString(Context ctx, String key, String defValue) {
if (null == ctx) {
return "";
}
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
return sp.getString(key, defValue);
}
public static void putInt(Context ctx, String key, int value) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
sp.edit().putInt(key, value).commit();
}
public static int getInt(Context ctx, String key, int defValue) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
return sp.getInt(key, defValue);
}
public static void remove(Context ctx, String key) {
if (null == ctx) {
return;
}
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
sp.edit().remove(key).commit();
}
/**
* writeObject 方法负责写入特定类的对象的状态,以便相应的 readObject 方法可以还原它
* 最后,用Base64.encode将字节文件转换成Base64编码保存在String中
*
* @param object 待加密的转换为String的对象
* @return String 加密后的String
*/
private static String Object2String(Object object) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(object);
String string = new String(Base64.encode(byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
objectOutputStream.close();
return string;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 使用Base64解密String,返回Object对象
*
* @param objectString 待解密的String
* @return object 解密后的object
*/
private static Object String2Object(String objectString) {
byte[] mobileBytes = Base64.decode(objectString.getBytes(), Base64.DEFAULT);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mobileBytes);
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(byteArrayInputStream);
Object object = objectInputStream.readObject();
objectInputStream.close();
return object;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 使用SharedPreference保存对象
*
* @param fileKey 储存文件的key
* @param key 储存对象的key
* @param saveObject 储存的对象
*/
public static void saveObj(Context context, String fileKey, String key, Object saveObject) {
SharedPreferences sharedPreferences = context.getSharedPreferences(fileKey, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
String string = Object2String(saveObject);
editor.putString(key, string);
editor.commit();
}
/**
* 获取SharedPreference保存的对象
*
* @param fileKey 储存文件的key
* @param key 储存对象的key
* @return object 返回根据key得到的对象
*/
public static Object getObj(Context context, String fileKey, String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences(fileKey, Activity.MODE_PRIVATE);
String string = sharedPreferences.getString(key, null);
if (string != null) {
Object object = String2Object(string);
return object;
} else {
return null;
}
}
}
使用示例:
//存
SpUtils.putString(LogInActivity.this, GlobalContants.TOKEN, loginInfo.getData().getToken());//token
SpUtils.putInt(LogInActivity.this, GlobalContants.USER_ID, loginInfo.getData().getUser_id());//user_id
//取
String token=SpUtils.getString(getActivity(), GlobalContants.TOKEN, "")
int userId=SpUtils.getInt(getActivity(), GlobalContants.USER_ID, -1)