您的位置:首页 > 其它

SharedPrefs工具类

2016-01-13 17:22 232 查看
SharedPrefs分装了全部的基本类型,还进行了对象的分装,对象分装一定要让对象实现Serializable让对象序列化。

package com.pa.manager.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Set;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

import com.lidroid.xutils.util.LogUtils;
import com.pa.manager.log.Log;

/**
* SharedPreferences封装类
*/
public class SharedPrefsUtil {
private static final String sharedName="sp";
static SharedPreferences sp = null;

public static SharedPreferences getSp(Context context) {
if (sp == null) {
sp = context.getSharedPreferences(sharedName, 0);
}
return sp;
}
public static SharedPreferences getSp( Context context,String sharedName) {
if (TextUtils.isEmpty(sharedName)|| sp== null) {
sp = context.getSharedPreferences(sharedName, 0);
}
return sp;
}
public static void clearallSP(Context context){
SharedPreferences.Editor spEdit = getSp(context).edit();
spEdit.clear();
spEdit.commit();
}
public static void clearTOkeySP(Context context,String key){
SharedPreferences.Editor spEdit = getSp(context).edit();
spEdit.clear();
spEdit.commit();
}

/**
* 查询缓存值
*
* @param context
* @param key
* @param id
*/
public static void putInt(Context context, String key, int id) {
SharedPreferences.Editor spEdit = getSp(context).edit();
spEdit.putInt(key, id);
spEdit.commit();
}

/**
* 查询缓存值
*
* @param context
* @param key
* @return
*/
public static int getInt(Context context, String key) {
return getSp(context).getInt(key, -1);
}

/**
* 查询缓存值
*
* @param context
* @param key
* @param str
*/
public static void putString(Context context, String key, String str) {
SharedPreferences.Editor spEdit = getSp(context).edit();
spEdit.putString(key, str);
spEdit.commit();
}

/**
* 获取string
*/
public static String getString(Context context, String key) {
return getSp(context).getString(key, null);
}

/**
* 获取Float
*/
public static float getFloat(Context context, String key) {
return getSp(context).getFloat(key, -1F);
}

/**
* 设置
*/
public static void putLong(Context context, String key, long l) {
SharedPreferences.Editor spEdit = getSp(context).edit();
spEdit.putLong(key, l);
spEdit.commit();
}

/**
*
*/
public static long getLong(Context context, String key) {
return getSp(context).getLong(key, -1L);
}

public static long getLong(Context context, String key, long defaultValue) {
return getSp(context).getLong(key, defaultValue);
}

/**
* 查询缓存值
*
* @param context
* @param key
* @param b
*/
public static void putBoolean(Context context, String key, boolean b) {
SharedPreferences.Editor spEdit = getSp(context).edit();
spEdit.putBoolean(key, b);
spEdit.commit();
}

/**
* 查询缓存值
*
* @param context
* @param key
* @return
*/
public static boolean getBoolean(Context context, String key) {
return getSp(context).getBoolean(key, false);
}

/**
* 缓存Set集合值
*
* @param context
* @param key
* @param ids
*/
public static void putSet(Context context, String key, Set<String> ids) {
SharedPreferences.Editor spEdit = getSp(context).edit();
spEdit.putStringSet(key, ids);
spEdit.commit();
}

public static void remove(Context context, String key) {
SharedPreferences.Editor spEdit = getSp(context).edit();
spEdit.remove(key);
spEdit.commit();
}

/**
* SharedPreferenc 存储对象
*
* @param context
* @param key
* @param obj
*/
public static void putObjectStream(Context context, String key, Object obj) {
ByteArrayOutputStream baos;
try {
baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
// Save user preferences. use Editor object to make changes.
SharedPreferences.Editor spEdit = getSp(context).edit();
String strObj = new String(Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT));
spEdit.putString(key, strObj);
spEdit.commit();
} catch (IOException e) {
Log.e("IOException", e.toString());
}
}

/**
* 从SharedPreferences文件流中获取对象
*
* @param context
* @param key
* @return
*/
public static Object getObjectStream(Context context, String key) {
try {
String paramBase64 = getSp(context).getString(key, null);
byte[] base64Bytes = Base64.decode(paramBase64.getBytes(), Base64.DEFAULT);
ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
Log.d("Exception", "" + e.toString());
}
return null;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: