SharedPreferences
調(diào)用putParam 就能保存String, Integer, Boolean, Float, Long類型的參數(shù)
調(diào)用getParam就能獲取到保存在手機(jī)里面的數(shù)據(jù)
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Base64;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
public class SPUtils {
/**
- 保存在手機(jī)里面的文件名
*/
public static final String FILE_NAME = "share_data";
/**
- 保存數(shù)據(jù)的方法,我們需要拿到保存數(shù)據(jù)的具體類型嘉蕾,然后根據(jù)類型調(diào)用不同的保存方法
*/
public static void putParam(Context context, String key, Object object) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, 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 Float) {
editor.putFloat(key, (Float) object);
} else if (object instanceof Long) {
editor.putLong(key, (Long) object);
} else {
editor.putString(key, object.toString());
}
SharedPreferencesCompat.apply(editor);
}
//保存對(duì)象
public static boolean setObjectToShare(Context context, Object object, String key) {
// TODO Auto-generated method stub
SharedPreferences share = PreferenceManager
.getDefaultSharedPreferences(context);
if (object == null) {
SharedPreferences.Editor editor = share.edit().remove(key);
return editor.commit();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
} catch (IOException e) {
e.printStackTrace();
return false;
}
// 將對(duì)象放到OutputStream中
// 將對(duì)象轉(zhuǎn)換成byte數(shù)組饥脑,并將其進(jìn)行base64編碼
String objectStr = new String(Base64.encode(baos.toByteArray(),
Base64.DEFAULT));
try {
baos.close();
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SharedPreferences.Editor editor = share.edit();
// 將編碼后的字符串寫到base64.xml文件中
editor.putString(key, objectStr);
return editor.commit();
}
//獲取對(duì)象
public static Object getObjectFromShare(Context context, String key) {
SharedPreferences sharePre = PreferenceManager
.getDefaultSharedPreferences(context);
try {
String wordBase64 = sharePre.getString(key, "");
// 將base64格式字符串還原成byte數(shù)組
if (wordBase64 == null || wordBase64.equals("")) { // 不可少专酗,否則在下面會(huì)報(bào)java.io.StreamCorruptedException
return null;
}
byte[] objBytes = Base64.decode(wordBase64.getBytes(),
Base64.DEFAULT);
ByteArrayInputStream bais = new ByteArrayInputStream(objBytes);
ObjectInputStream ois = new ObjectInputStream(bais);
// 將byte數(shù)組轉(zhuǎn)換成product對(duì)象
Object obj = ois.readObject();
bais.close();
ois.close();
return obj;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
- 得到保存數(shù)據(jù)的方法齐鲤,我們根據(jù)默認(rèn)值得到保存的數(shù)據(jù)的具體類型,然后調(diào)用相對(duì)于的方法獲取值
*/
public static Object getParam(Context context, String key, Object defaultObject) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, 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;
}
/**
- 移除某個(gè)key值已經(jīng)對(duì)應(yīng)的值
*/
public static void remove(Context context, String key) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.remove(key);
SharedPreferencesCompat.apply(editor);
}
/**
- 清除所有數(shù)據(jù)
*/
public static void clear(Context context) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.clear();
SharedPreferencesCompat.apply(editor);
}
/**
- 查詢某個(gè)key是否已經(jīng)存在
*/
public static boolean contains(Context context, String key) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
return sp.contains(key);
}
/**
- 返回所有的鍵值對(duì)
*/
public static Map<String, ?> getAll(Context context) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
return sp.getAll();
}
/**
- 創(chuàng)建一個(gè)解決SharedPreferencesCompat.apply方法的一個(gè)兼容類
*/
private static class SharedPreferencesCompat {
private static final Method sApplyMethod = findApplyMethod();
/**
* 反射查找apply的方法
*/
@SuppressWarnings({ "unchecked", "rawtypes" }) private static Method findApplyMethod() {
try {
Class clz = SharedPreferences.Editor.class;
return clz.getMethod("apply");
} catch (NoSuchMethodException e) {
}
return null;
}
/**
* 如果找到則使用apply執(zhí)行昂秃,否則使用commit
*/
public static void apply(SharedPreferences.Editor editor) {
try {
if (sApplyMethod != null) {
sApplyMethod.invoke(editor);
return;
}
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
editor.commit();
}
}
}