美文网首页
SharedPreferences工具类

SharedPreferences工具类

作者: VinPin | 来源:发表于2017-07-15 22:44 被阅读158次

    创建单例

    单例模式

    常用的方法

    public static void putBoolean(String key, boolean value, Context context) {    
        SharedPreference.getInstance(context).edit().putBoolean(key, value).apply();
    }
    
    public static boolean getBoolean(String key, boolean defValue, Context context) {
        return SharedPreference.getInstance(context).getBoolean(key, defValue);
    }
    
    public static void putString(String key, String value, Context context) {
        SharedPreference.getInstance(context).edit().putString(key, value).apply();
    }
    
    public static String getString(String key, String defValue, Context context) {
        return SharedPreference.getInstance(context).getString(key, defValue);
    }
    
    public static void putInt(String key, int value, Context context) {
        SharedPreference.getInstance(context).edit().putInt(key, value).apply();
    }
    
    public static int getInt(String key, int defValue, Context context) {
        return SharedPreference.getInstance(context).getInt(key, defValue);
    }
    

    移除某个key值已经对应的值

    public static void remove(String key, Context context) {
        SharedPreference.getInstance(context).edit().remove(key).apply();
    }
    

    清除所有内容

    public static void clear(Context context) {
        SharedPreference.getInstance(context).edit().clear().apply();
    }
    

    完整代码

    /**
     * 保存到本地的配置文件
     *
     * @author VinPin
     */
    public class SharedPreference {
    
        private static String FILLNAME = "config";// 文件名称
        private static SharedPreferences mSharedPreferences = null;
    
        public final static String PRE_ACCOUNT = "account";
        public final static String PRE_USERID = "userid";
        public final static String PRE_TOKENID = "tokenid";
        public final static String PRE_USERNAME = "username";
        public final static String PRE_USEREMAIL = "useremail";
        public final static String PRE_PHONE = "phone";
    
        /**
         * 单例模式
         */
        public static synchronized SharedPreferences getInstance(Context context) {
            if (mSharedPreferences == null) {
                mSharedPreferences = context.getApplicationContext().getSharedPreferences(FILLNAME, Context.MODE_PRIVATE);
            }
            return mSharedPreferences;
        }
    
        /**
         * SharedPreferences常用的10个操作方法
         */
        public static void putBoolean(String key, boolean value, Context context) {
            SharedPreference.getInstance(context).edit().putBoolean(key, value).apply();
        }
    
        public static boolean getBoolean(String key, boolean defValue, Context context) {
            return SharedPreference.getInstance(context).getBoolean(key, defValue);
        }
    
        public static void putString(String key, String value, Context context) {
            SharedPreference.getInstance(context).edit().putString(key, value).apply();
        }
    
        public static String getString(String key, String defValue, Context context) {
            return SharedPreference.getInstance(context).getString(key, defValue);
        }
    
        public static void putInt(String key, int value, Context context) {
            SharedPreference.getInstance(context).edit().putInt(key, value).apply();
        }
    
        public static int getInt(String key, int defValue, Context context) {
            return SharedPreference.getInstance(context).getInt(key, defValue);
        }
    
        /**
         * 移除某个key值已经对应的值
         */
        public static void remove(String key, Context context) {
            SharedPreference.getInstance(context).edit().remove(key).apply();
        }
    
        /**
         * 清除所有内容
         */
        public static void clear(Context context) {
            SharedPreference.getInstance(context).edit().clear().apply();
        }
    }
    

    相关文章

      网友评论

          本文标题:SharedPreferences工具类

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