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;
    }

你可能感兴趣的:(android)