SharePreference封装使用

/**
 * Created by malei on 2017/2/8.
 */

public class SharePreferenceUtils {

    private static final String NAME = "properties";

    private static SharedPreferences get(Context context, String userId) {
        return context.getSharedPreferences(NAME + userId, Context.MODE_PRIVATE);
    }

    /******************************
     * 有userId的
     *************************************************/

    public static boolean put(Context context, String userId, String key, String value) {
        return get(context, userId).edit().putString(key, value).commit();
    }

    public static String getString(Context context, String userId, String key, String defValue) {
        return get(context, userId).getString(key, defValue);
    }


    /*********************************
     * 没有userId的
     **********************************************/

    public static boolean put(Context context, String key, String value) {
        return get(context, "").edit().putString(key, value).commit();
    }

    public static String getString(Context context, String key) {
        return get(context, "").getString(key, "");
    }

    public static String getString(Context context, String key, String defValue) {
        return get(context, "").getString(key, defValue);
    }

    /*********************************
     * 移除
     *********************************************/
    public static boolean remove(Context context, String key) {
        return get(context, "").edit().remove(key).commit();
    }

    public static boolean remove(Context context, String userId, String key) {
        return get(context, userId).edit().remove(key).commit();
    }

    /*********************************
     * 保存和获取对象 只支持Serializable
     *********************************************/
    //保存对象
    public static boolean saveObjectToShare(Context context, String key,Object object) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        if (object == null) {
            SharedPreferences.Editor editor = sp.edit().remove(key);
            return editor.commit();
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        // 将对象放到OutputStream中
        // 将对象转换成byte数组,并将其进行base64编码
        String objectStr = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
        try {
            baos.close();
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        SharedPreferences.Editor editor = sp.edit();
        // 将编码后的字符串写到base64.xml文件中
        editor.putString(key, objectStr);
        return editor.commit();
    }

    public static Object getObjectToShare(Context context, String key) {
        SharedPreferences sharePre = PreferenceManager.getDefaultSharedPreferences(context);
        try {
            String wordBase64 = sharePre.getString(key, "");
            // 将base64格式字符串还原成byte数组
            if (wordBase64 == null || wordBase64.equals("")) { // 不可少,否则在下面会报java.io.StreamCorruptedException
                return null;
            }
            byte[] objBytes = Base64.decode(wordBase64.getBytes(), Base64.DEFAULT);
            ByteArrayInputStream bais = new ByteArrayInputStream(objBytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            // 将byte数组转换成product对象
            Object obj = ois.readObject();
            bais.close();
            ois.close();
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值