实用代码块记录5

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值