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; } }
相关文章推荐
- 文字不换行使用等于号
- spring mvc快速搭建——Hello World!
- POJ-3080 -求n个字符串最长公共子串-暴力枚举
- UVa146
- 【装载】hibernate的三种状态
- 机制设计专栏(1)-从social choice 谈起
- 【Unity Shader】浅析Unity shader中RenderType的作用及_CameraDepthNormalsTexture
- 《JS高级程序设计》读书笔记
- jvm判断对象是否存活
- 手机APP对你的生活到底有多大的影响
- 编辑距离-Levenshtein距离
- Hsql中In没有1000的限制
- ps图层填充颜色——先选好颜色,再选中需要填充颜色或者修改颜色的图层,最后按住Alt+Delete键。完成。
- web简单的整体测试
- 【Tyvj1038】忠诚
- 蓝桥杯<序列求和>
- PL/SQL教程之一:简介
- Python扫描文件夹&&分析文件
- 无法激活服务,因为它不支持 ASP.NET 兼容性
- 设计模式笔记--解释器模式