package com.xjf.repository.utils; import android.content.Context; import android.content.SharedPreferences; import java.util.Map; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2016/8/2 * desc : SP相关工具类 * </pre> */ public class SPUtils { private SharedPreferences sp; private SharedPreferences.Editor editor; /** * SPUtils构造函数 * * @param context 上下文 * @param spName spName */ public SPUtils(Context context, String spName) { sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE); editor = sp.edit(); editor.apply(); } /** * SP中写入String类型value * * @param key 键 * @param value 值 */ public void putString(String key, String value) { editor.putString(key, value).apply(); } /** * SP中读取String * * @param key 键 * @return 存在返回对应值,不存在返回默认值{@code null} */ public String getString(String key) { return getString(key, null); } /** * SP中读取String * * @param key 键 * @param defaultValue 默认值 * @return 存在返回对应值,不存在返回默认值{@code defaultValue} */ public String getString(String key, String defaultValue) { return sp.getString(key, defaultValue); } /** * SP中写入int类型value * * @param key 键 * @param value 值 */ public void putInt(String key, int value) { editor.putInt(key, value).apply(); } /** * SP中读取int * * @param key 键 * @return 存在返回对应值,不存在返回默认值-1 */ public int getInt(String key) { return getInt(key, -1); } /** * SP中读取int * * @param key 键 * @param defaultValue 默认值 * @return 存在返回对应值,不存在返回默认值{@code defaultValue} */ public int getInt(String key, int defaultValue) { return sp.getInt(key, defaultValue); } /** * SP中写入long类型value * * @param key 键 * @param value 值 */ public void putLong(String key, long value) { editor.putLong(key, value).apply(); } /** * SP中读取long * * @param key 键 * @return 存在返回对应值,不存在返回默认值-1 */ public long getLong(String key) { return getLong(key, -1L); } /** * SP中读取long * * @param key 键 * @param defaultValue 默认值 * @return 存在返回对应值,不存在返回默认值{@code defaultValue} */ public long getLong(String key, long defaultValue) { return sp.getLong(key, defaultValue); } /** * SP中写入float类型value * * @param key 键 * @param value 值 */ public void putFloat(String key, float value) { editor.putFloat(key, value).apply(); } /** * SP中读取float * * @param key 键 * @return 存在返回对应值,不存在返回默认值-1 */ public float getFloat(String key) { return getFloat(key, -1f); } /** * SP中读取float * * @param key 键 * @param defaultValue 默认值 * @return 存在返回对应值,不存在返回默认值{@code defaultValue} */ public float getFloat(String key, float defaultValue) { return sp.getFloat(key, defaultValue); } /** * SP中写入boolean类型value * * @param key 键 * @param value 值 */ public void putBoolean(String key, boolean value) { editor.putBoolean(key, value).apply(); } /** * SP中读取boolean * * @param key 键 * @return 存在返回对应值,不存在返回默认值{@code false} */ public boolean getBoolean(String key) { return getBoolean(key, false); } /** * SP中读取boolean * * @param key 键 * @param defaultValue 默认值 * @return 存在返回对应值,不存在返回默认值{@code defaultValue} */ public boolean getBoolean(String key, boolean defaultValue) { return sp.getBoolean(key, defaultValue); } /** * 获取SP中所有键值对 * * @return Map对象 */ public Map<String, ?> getAll() { return sp.getAll(); } /** * 从SP中移除该key * * @param key 键 */ public void remove(String key) { editor.remove(key).apply(); } /** * 判断SP中是否存在该key * * @param key 键 * @return {@code true}: 存在<br>{@code false}: 不存在 */ public boolean contains(String key) { return sp.contains(key); } /** * 清除SP中所有数据 */ public void clear() { editor.clear().apply(); } }