美文网首页
03.SharedPreUtil

03.SharedPreUtil

作者: Jsonzhang | 来源:发表于2018-01-03 15:13 被阅读21次

    一直想写下SharePreUtil,结合网上和自己的东西,最终结果就是这样的,大家看看怎么样?

    public class SharedPreUtil {
    
        private static SharedPreUtil singleTon;
        private static SharedPreferences sp;
        private static SharedPreferences.Editor editor;
    
        private static final String SHARED_NAME = "SHARED_NAME";
        public static final String STRING = "String";
        public static final String INTEGER = "Integer";
        public static final String BOOLEAN = "Boolean";
        public static final String FLOAT = "Float";
        public static final String LONG = "Long";
    
    
        public static SharedPreUtil builder(Context context) {
            if (singleTon == null) {
                synchronized (SharedPreUtil.class) {
                    if (singleTon == null) {
                        singleTon = new SharedPreUtil();
                    }
                }
            }
            if (sp == null) {
                sp = context.getSharedPreferences(SHARED_NAME, Context.MODE_PRIVATE);
                editor = sp.edit();
            }
            return singleTon;
        }
    
    
        public void save(String key, @NonNull Object object) {
            if (object == null) object = "";
            String type = object.getClass().getSimpleName();
            if (STRING.equals(type)) {
                editor.putString(key, (String) object);
            } else if (INTEGER.equals(type)) {
                editor.putInt(key, (Integer) object);
            } else if (BOOLEAN.equals(type)) {
                editor.putBoolean(key, (Boolean) object);
            } else if (FLOAT.equals(type)) {
                editor.putFloat(key, (Float) object);
            } else if (LONG.equals(type)) {
                editor.putLong(key, (Long) object);
            }
            editor.apply();
        }
    
    
        public Object get(String key, @NonNull Object defaultObject) {
            String type = defaultObject.getClass().getSimpleName();
            if (STRING.equals(type)) {
                return sp.getString(key, (String) defaultObject);
            } else if (INTEGER.equals(type)) {
                return sp.getInt(key, (Integer) defaultObject);
            } else if (BOOLEAN.equals(type)) {
                return sp.getBoolean(key, (Boolean) defaultObject);
            } else if (FLOAT.equals(type)) {
                return sp.getFloat(key, (Float) defaultObject);
            } else if (FLOAT.equals(type)) {
                return sp.getLong(key, (Long) defaultObject);
            }
            return null;
        }
    
    
        //移除某个值
        public void remove(String key) {
            editor.remove(key);
            editor.apply();
        }
    
        //清楚所有值
        public void clear() {
            editor.clear();
            editor.apply();
        }
    
        //是否包含这个key
        public boolean contains(String key) {
            return sp.contains(key);
        }
    }
    
    //SharedPreference存储
     SharedPreUtil.builder(this).save("zsl-s", "好");
     SharedPreUtil.builder(this).save("zsl-b", true);
     SharedPreUtil.builder(this).save("zsl-i", 100);
     SharedPreUtil.builder(this).save("zsl-f", 10f);
     SharedPreUtil.builder(this).save("zsl-l", 99l);
    
    //取值和清除等操作
    String s = (String) SharedPreUtil.builder(this).get("zsl-s", "李四");
    boolean b = SharedPreUtil.builder(this).contains("zsl-b");
    SharedPreUtil.builder(this).remove("zsl-s");
    SharedPreUtil.builder(this).clear();
    

    相关文章

      网友评论

          本文标题:03.SharedPreUtil

          本文链接:https://www.haomeiwen.com/subject/rwbhnxtx.html