当前位置:   article > 正文

SharePreference封装使用_shareperference封装

shareperference封装
/**
 * 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;
    }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/思考机器/article/detail/60753?site
推荐阅读
相关标签
  

闽ICP备14008679号