工具类_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();
    }
}

posted @ 2017-04-17 10:39  ytymz  阅读(441)  评论(0编辑  收藏  举报