美文网首页
PreferencesUtils工具类,简化preference

PreferencesUtils工具类,简化preference

作者: zebbin | 来源:发表于2016-05-20 17:50 被阅读0次
    public class PreferencesUtils {
    
        private Context context;
    
        public PreferencesUtils(Context context) {
            this.context = context;
        }
    
        public boolean putInt(String key, int value) {
            boolean flag = context.getSharedPreferences(Constant.PREFERENCE_NAME, Context.MODE_PRIVATE).edit().putInt(key, value).commit();
            return flag;
        }
    
        public int getInt(String key, int defValue) {
            return context.getSharedPreferences(Constant.PREFERENCE_NAME, Context.MODE_PRIVATE).getInt(key, defValue);
        }
    
        public boolean putLong(String key, long value) {
            boolean flag = context.getSharedPreferences(Constant.PREFERENCE_NAME, Context.MODE_PRIVATE).edit().putLong(key, value).commit();
            return flag;
        }
    
        public long getLong(String key, long defValue) {
            return context.getSharedPreferences(Constant.PREFERENCE_NAME, Context.MODE_PRIVATE).getLong(key, defValue);
        }
    
        public boolean putString(String key, String value) {
            return context.getSharedPreferences(Constant.PREFERENCE_NAME, Context.MODE_PRIVATE).edit().putString(key, value).commit();
        }
    
        public String getString(String key, String defValue) {
            return context.getSharedPreferences(Constant.PREFERENCE_NAME, Context.MODE_PRIVATE).getString(key, defValue);
        }
    
        public boolean putBoolean(String key, boolean value) {
            return context.getSharedPreferences(Constant.PREFERENCE_NAME, Context.MODE_PRIVATE).edit().putBoolean(key, value).commit();
        }
    
        public boolean getBoolean(String key, boolean defValue) {
            return context.getSharedPreferences(Constant.PREFERENCE_NAME, Context.MODE_PRIVATE).getBoolean(key, defValue);
        }
    
        public boolean clearAllValue() {
            return context.getSharedPreferences(Constant.PREFERENCE_NAME, Context.MODE_PRIVATE).edit().clear().commit();
        }
    }
    

    相关文章

      网友评论

          本文标题:PreferencesUtils工具类,简化preference

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