SharedPreferences保存工具类
日常开发中,经常需要SharedPreferences保存数据到本地,以方便本地使用,保存的数据大致有Int ,String、boolean、对象等
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.io.Serializable;
/**
* ***********************************************************************************************************************************
* 修改日期 修改人 任务名称 功能或Bug描述
* 2018/9/7 18:49 MUZI102 SharedPreferences工具类
* ***********************************************************************************************************************************
*/
public class PrefShare {
private SharedPreferences preferences;
private Context context;
private static PrefShare prefShare;
private PrefShare(Context context) {
this.context = context;
preferences = PreferenceManager
.getDefaultSharedPreferences(this.context);
}
public static PrefShare getInstance(Context context) {
if (prefShare == null) {
synchronized (PrefShare.class) {
if (prefShare == null)
prefShare = new PrefShare(context);
}
}
return prefShare;
}
private synchronized boolean saveString(String key, String value) {
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
return editor.commit();
}
private synchronized String getString(String key) {
return preferences.getString(key, null);
}
private synchronized boolean saveInt(String key, int value) {
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(key, value);
return editor.commit();
}
private synchronized int getInt(String key) {
return preferences.getInt(key, 0);
}
private synchronized boolean putIsBoolean(Context context, String key, boolean value) {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
return editor.commit();
}
private synchronized boolean getBoolean(Context context, String key, boolean defValue) {
SharedPreferences.Editor editor = preferences.edit();
return preferences.getBoolean(key, defValue);
}
private synchronized boolean saveDouble(String key, double value) {
return saveString(key, String.valueOf(value));
}
private synchronized double getDouble(String key) {
String value = getString(key);
if (null != value) {
return Double.valueOf(value);
} else {
return 0;
}
}
private synchronized boolean saveSerializableObject(String key, Object object) {
if (object instanceof Serializable) {
SharedPreferences.Editor editor = preferences.edit();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(object);
String temp = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
editor.putString(key, temp);
return editor.commit();
} catch (IOException e) {
e.printStackTrace();
return false;
}
} else {
return false;
}
}
/**
* 使用案例
* private TextView textView1;
* textView1 = findView(R.id.textView1);
*
* @param key
* @param <T>
* @return
*/
private synchronized <T> T getSerializableObject(String key) {
String temp = preferences.getString(key, "");
ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decode(temp.getBytes(), Base64.DEFAULT));
T o = null;
try {
ObjectInputStream ois = new ObjectInputStream(bais);
o = (T) ois.readObject();
} catch (IOException e) {
} catch (ClassNotFoundException e1) {
}
return o;
}
/**
* 使用代码
* ***********************************************************************************************************************************
*/
/**
* 保存用户名
*
* @param username
* @return
*/
public boolean saveUserName(String username) {
return saveString("username", username);
}
/**
* 获取用户名
*
* @return
*/
public String getUserName() {
return getString("username");
}
}