zoukankan      html  css  js  c++  java
  • 工具类_SharedPreferencesUtils

    import android.app.Application;
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;

    /**
     * 保持配置信息的工具类 2016-08-24
     *
     * @author lipanquan
     *
     */
    public final class SharedPreferencesUtils {

        private void SharedPreferencesUtils(){}

        /**
         * 私有构造(单例)
         *
         * @param application
         *            上下文
         */
        private SharedPreferencesUtils(Application application) {
            sp = application.getSharedPreferences("setting", Context.MODE_PRIVATE);
        }

        /**
         * 本类对象
         */
        private static SharedPreferencesUtils instance;

        /**
         * SharedPreferences对象
         */
        private SharedPreferences sp;

        /**
         * 对外提供获取本类对象的方法
         *
         * @param application
         *            上下文
         * @return SharedPreferences对象
         */
        public static SharedPreferencesUtils getInstance(Application application) {
            if (IsNull.isNull(instance)) {
                instance = new SharedPreferencesUtils(application);
            }
            return instance;
        }

        /**
         * 读取字符串
         *
         * @param key
         *            键
         * @return String类型的值
         */
        public String getString(String key) {
            return sp.getString(key, "");
        }

        /**
         * 保持字符串
         *
         * @param key
         *            键
         * @param value
         *            值
         */
        public void putString(String key, String value) {
            Editor edit = sp.edit();
            edit.putString(key, value);
            edit.commit();
        }

        /**
         * 读取布尔类型数据
         *
         * @param key
         *            键
         * @return Boolean类型的值
         */
        public Boolean getBoolean(String key) {
            return sp.getBoolean(key, false);
        }
        
        /**
         * 读取布尔类型数据
         *
         * @param key
         *            键
         * @param defValue
         *            默认值
         * @return Boolean类型的值
         */
        public Boolean getBoolean(String key, boolean defValue) {
            return sp.getBoolean(key, defValue);
        }

        /**
         * 保存布尔类型的数据
         *
         * @param key
         *            键
         * @param value
         *            值
         */
        public void putBoolean(String key, Boolean value) {
            Editor edit = sp.edit();
            edit.putBoolean(key, value);
            edit.commit();
        }

        /**
         * 读取整型类型数据
         *
         * @param key
         *            键
         * @return Int类型的值
         */
        public Integer getInt(String key) {
            return sp.getInt(key, 0);
        }

        /**
         * 保存整型类型的数据
         *
         * @param key
         *            键
         * @param value
         *            值
         */
        public void putInt(String key, Integer value) {
            Editor edit = sp.edit();
            edit.putInt(key, value);
            edit.commit();
        }

        /**
         * 读取浮点型类型数据
         *
         * @param key
         *            键
         * @return Float类型的值
         */
        public Float getFloat(String key) {
            return sp.getFloat(key, 0);
        }

        /**
         * 保存浮点型类型的数据
         *
         * @param key
         *            键
         * @param value
         *            值
         */
        public void putFloat(String key, Float value) {
            Editor edit = sp.edit();
            edit.putFloat(key, value);
            edit.commit();
        }

        /**
         * 读取长整型类型数据
         *
         * @param key
         *            键
         * @return Long类型的值
         */
        public Long getLong(String key) {
            return sp.getLong(key, 0);
        }

        /**
         * 保存长整型类型的数据
         *
         * @param key
         *            键
         * @param value
         *            Long值
         */
        public void putLong(String key, Long value) {
            Editor edit = sp.edit();
            edit.putLong(key, value);
            edit.commit();
        }
    }

  • 相关阅读:
    微信支付遇到的坑
    linux搭载discuz
    关于php自带的访问服务器xml的方法的坑
    yii的验证码
    菜鸟配置阿里云
    ajax(通过jQuery实现)
    leetcode95 Unique Binary Search Trees II
    leetcode96 Unique Binary Search Trees
    leetcode98 Validate Binary Search Tree
    leetcode-99 Recover_Binary_Search_Tree
  • 原文地址:https://www.cnblogs.com/wf-l5201314/p/6721723.html
Copyright © 2011-2022 走看看