送你们一个SharedPreferences 的工具类

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by MnyZhao on 2017/11/15.
 * @author MnyZhao
 */

public class SpUtils {
    private static SharedPreferences preferences;

    public static void getPreference(Context context) {
        if (preferences == null) {
            preferences = context.getSharedPreferences("mysp", Context.MODE_PRIVATE);
        }
    }
    /**
     *
     * @param context  上下文 最好用Application的context
     * @param key 属性名
     * @param value 属性值
     */
    public static void putBoolean(Context context, String key, boolean value) {
        getPreference(context);
        preferences.edit().putBoolean(key, value).commit();
    }
    /**
     *
     * @param context  上下文 最好用Application的context
     * @param key 属性名
     * @param defaultValue 属性值 默认
     */
    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        getPreference(context);
        return preferences.getBoolean(key, defaultValue);
    }

    /**
     *
     * @param context  上下文 最好用Application的context
     * @param key 属性名
     * @param value 属性值
     */
    public static void putFloat(Context context, String key, float value) {
        getPreference(context);
        preferences.edit().putFloat(key, value).commit();
    }
    /**
     *
     * @param context  上下文 最好用Application的context
     * @param key 属性名
     * @param defaultValue 属性值 默认
     */
    public static float getFloat(Context context, String key, float defaultValue) {
        getPreference(context);
        return preferences.getFloat(key, defaultValue);
    }

    /**
     *
     * @param context  上下文 最好用Application的context
     * @param key 属性名
     * @param value 属性值
     */
    public static void putString(Context context, String key, String value) {
        getPreference(context);
        preferences.edit().putString(key, value).commit();
    }
    /**
     *
     * @param context  上下文 最好用Application的context
     * @param key 属性名
     * @param defaultValue 属性值 默认
     */
    public static String getString(Context context, String key, String defaultValue) {
        getPreference(context);
        return preferences.getString(key, defaultValue);
    }

    /**
     *
     * @param context  上下文 最好用Application的context
     * @param key 属性名
     * @param value 属性值
     */
    public static void putLong(Context context, String key, long value) {
        getPreference(context);
        preferences.edit().putLong(key, value).commit();
    }
    /**
     *
     * @param context  上下文 最好用Application的context
     * @param key 属性名
     * @param defaultValue 属性值 默认
     */
    public static long getLong(Context context, String key, long defaultValue) {
        getPreference(context);
        return preferences.getLong(key, defaultValue);
    }
    /**
     *
     * @param context  上下文 最好用Application的context
     * @param key 属性名
     * @param value 属性值
     */
    public static void putInt(Context context, String key, int value) {
        getPreference(context);
        preferences.edit().putInt(key, value).commit();
    }
    /**
     *
     * @param context  上下文 最好用Application的context
     * @param key 属性名
     * @param defaultValue 属性值 默认
     */
    public static int getInt(Context context, String key, int defaultValue) {
        getPreference(context);
        return preferences.getInt(key, defaultValue);
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值