实用代码块记录5
来源:互联网 发布:加工中心怎么手动编程 编辑:程序博客网 时间:2024/06/09 15:23
PreferenceUtils
import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;/** * SharedPreferences配置保存工具 * */public class PreferenceUtils { private SharedPreferences mShared; /** * 构建配置工具 * * @param context * 设备上下文环境 * @param name * 配置文件名 */ public PreferenceUtils(Context context, String name) { mShared = context.getSharedPreferences(name, 0); } /** * 保存boolean值 * * @param key * 键 * @param value * 值 */ public void put(String key, boolean value) { Editor editor = mShared.edit(); editor.putBoolean(key, value); editor.commit(); } /** * 获取boolean值 * * @param key * 键 * @return 查找不到值,默认返回false */ public boolean getBoolean(String key) { return getBoolean(key, false); } /** * 获取boolean值 * * @param key * 键 * @param defValue * 默认返回值 * @return 值 */ public boolean getBoolean(String key, boolean defValue) { return mShared.getBoolean(key, defValue); } /** * 保存float值 * * @param key * 键 * @param value * 值 */ public void put(String key, float value) { Editor editor = mShared.edit(); editor.putFloat(key, value); editor.commit(); } /** * 获取float值 * * @param key * 键 * @return 查找不到值,默认返回0 */ public float getFloat(String key) { return getFloat(key, 0); } /** * 获取float值 * * @param key * 键 * @param defValue * 默认返回值 * @return 值 */ public float getFloat(String key, float defValue) { return mShared.getFloat(key, defValue); } /** * 保存int值 * * @param key * 键 * @param value * 值 */ public void put(String key, int value) { Editor editor = mShared.edit(); editor.putInt(key, value); editor.commit(); } /** * 获取int值 * * @param key * 键 * @return 查找不到值,默认返回0 */ public int getInt(String key) { return getInt(key, 0); } /** * 获取int值 * * @param key * 键 * @param defValue * 默认返回值 * @return 值 */ public int getInt(String key, int defValue) { return mShared.getInt(key, defValue); } /** * 保存long值 * * @param key * 键 * @param value * 值 */ public void put(String key, long value) { Editor editor = mShared.edit(); editor.putLong(key, value); editor.commit(); } /** * 获取long值 * * @param key * 键 * @return 查找不到值,默认返回0 */ public long getLong(String key) { return getLong(key, 0); } /** * 获取long值 * * @param key * 键 * @param defValue * 默认返回值 * @return 值 */ public long getLong(String key, long defValue) { return mShared.getLong(key, defValue); } /** * 保存String值 * * @param key * 键 * @param value * 值 */ public void put(String key, String value) { Editor editor = mShared.edit(); editor.putString(key, value); editor.commit(); } /** * 获取String值 * * @param key * 键 * @return 查找不到值,默认返回null */ public String getString(String key) { return getString(key, null); } /** * 获取String值 * * @param key * 键 * @param defValue * 默认返回值 * @return 值 */ public String getString(String key, String defValue) { return mShared.getString(key, defValue); } /** * 保存序列化对象 * * @param key * 键 * @param value * 值 */ public void put(String key, Serializable value) { try { ByteArrayOutputStream toByte = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(toByte); oos.writeObject(value); String str = new String(SecurityUtils.encodeBASE64(toByte.toByteArray())); Editor editor = mShared.edit(); editor.putString(key, str); editor.commit(); } catch (IOException e) { } } /** * 获取序列化对象 * * @param key * 键 * @return 查找不到值,默认返回null */ public Object get(String key) { return get(key, null); } /** * 获取序列化对象 * * @param key * 键 * @param defValue * 默认返回值 * @return 值 */ public Object get(String key, Serializable defValue) { String str = mShared.getString(key, null); if (str == null) { return defValue; } try { byte[] baseBytes = SecurityUtils.decodeBASE64(str).getBytes(); ByteArrayInputStream fromeByte = new ByteArrayInputStream(baseBytes); ObjectInputStream ois = new ObjectInputStream(fromeByte); return ois.readObject(); } catch (Exception e) { } return defValue; } /** * 清空保存的数据 */ public void clear() { Editor editor = mShared.edit(); editor.clear(); editor.commit(); }}
0 0
- 实用代码块记录5
- 实用代码块记录1
- 实用代码块记录2
- 实用代码块记录3
- 实用代码块记录4
- 实用代码块记录6
- 实用代码块记录7
- 实用代码块记录8
- 实用代码块记录9
- 实用代码块记录10
- android实用代码块
- Shell 实用代码块
- 实用代码块
- 块代码实用入门指南
- 数组实用代码块简记
- android超实用代码块
- [代码块]记录
- 实用代码片段---持续记录
- Codeforces Round #360 (Div. 1)C - The Values You Can Make
- 算法探究之选择排序
- 用spark(spark-shell),从本地文件创建一个RDD
- JAVA第二课——事件监听以及画板的制作
- Codeforces 618 . A Slime Combining
- 实用代码块记录5
- VC之fseek函数、ftell函数和rewind函数
- cocos2d-lua android解决启动时因为资源加载而黑屏的问题
- Linux上的C程序不同文件调用
- LeetCode 068 Text Justification
- 实用代码块记录6
- 日常记录(三)更换Hexo主题
- 重新设置MySQL的密码
- ClassLoader和Class.forName