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

  • 相关阅读:
    java学习day51--DB旅游生态系统-项目搭建
    java学习day50--Spring Boot发送邮件任务
    java学习day49--yaml语法以及注入
    java学习day49--JSON扩展(详细)
    java学习day49--JS数组遍历
    java学习day49-基于AJAX实践操作(活动模块)
    java学习day48--Ajax技术--原生
    denied: requested access to the resource is denied
    VMware虚拟机网络适配器三种网络连接模式
    MySQL8.0.19最新版本rpm包和源码包百度地址
  • 原文地址:https://www.cnblogs.com/wf-l5201314/p/6721723.html
Copyright © 2011-2022 走看看