SharedPreference性能优化看这一篇就够了

发布时间:2019/6/12 14:36:04最新文章

点击上方蓝字关注?????



来源:? 上帝之手

https://juejin.im/post/5cf7756d6fb9a07eed34edff?utm_source=gold_browser_extension

前言

SharedPreference是Android系统中一种简单的、轻量级的文件存储,它是一种持久化的存储方式,以名称/值对(NVP)机制存放在xml中map根标签下,正如其名,它比较适合一些简单数据的存储,用于保存Int、long、boolean、String、Float、Set这些数据类型,可以在data/data/应用程序/shared_prefs的目录下可以查找到保存的xml文件。

使用方式

1.获取SharedPreference对象

SharedPreferences sp = context.getSharedPreferences(PREFERENCES_NAME,Context.MODE_PRIVATE);

2.获取 SharedPreferences.Editor

SharedPreferences.Editor edit = sp.edit();

3.存储数据

edit.putString(String key,String value);
edit.putStringSet(String key, Set values);
edit.putLong(String key,long value);
edit.putFloat(String key,float value);
edit.putBoolean(String key,boolean value);
edit.putInt(String key,int value);
edit.commit();//同步写入,频繁读取会阻塞主线程,引起ANR
edit.apply();//异步写入,不关心结果,官方推荐,速度与性能较好

4.获取数据

sp.getString(String key,String value);
sp.getStringSet(String key, Set values);
sp.getLong(String key,long value);
sp.getFloat(String key,float value);
sp.getBoolean(String key,boolean value);
sp.getInt(String key,int value);

耗时统计及优化建议

初始化
1.建议在Application中初始化,重写attachBaseContext方法,SharedPreference的context传入Application对象即可,最好使用单例,不必每次都获取Sp对象,减少开销。
2.如果项目中使用了MultiDex,存在分包,请在分包前即MultiDex.install()之前或者在multidex执行的这段时间,cpu是利用不满的,我们没有办法充分利用CPU的原因,是因为如果我们在Multidex之前执行一些操作,我们很有可能因为这样一些操作的类或者是相关的类不在我们的主dex当中,在四点几的类中会直接崩溃,但是由于sharePreference不会产生这种崩溃,是由于他是系统的类。

使用
1.请不要使用SharedPreference存储大文件及存储大量的key和value,这样的话会造成界面卡顿或者ANR比较占内存,记住它是简单存储,如果有类似的需求请考虑数据库、磁盘文件存储等等。
2.推荐使用apply进行存储,这也是官方推荐,当读入内存后,因为它是异步写入磁盘的,所以效率上会比commit好,当然如果你对存储的结果非常关心的话那就使用commit吧。
3.请不要频繁使用apply与commit,如果存在这样的问题,请合并一次性apply与commit,可以参考封装一map的结合,一次性提交,因为SharedPreference可能会存在IO瓶颈和锁性能差的问题。
4.尽量不要存放Json及html,数据少还可以接受,大量的话请放弃。
5.跨进程操作不要使用MULTI_PROCESS标志,而是使用contentprovide等进程间通信的方式。
6.如果你的项目对于存储性能要求非常高的情况,可以考虑放弃系统的SharedPreference存储,可以推荐使用腾讯的高性能组件MMKV,目前超过7k+的star。

封装工具类

封装是基于Blankj的AndroidUtilCode进行的部分改造,增加了提交Map的操作,方便一次性commit与apply,操作时请直接使用SpHelpUtils辅助类。

public class SpHelpUtils {
/**
* 简单存储工具类
*/
private static SpUtils sDefaultSpUtils;

/**
* Set the default instance of {@link SpUtils}.
*
* @param spUtils The default instance of {@link SpUtils}.
*/
public static void setDefaultSpUtils(final SpUtils spUtils) {
sDefaultSpUtils = spUtils;
}

/**
* Put the string value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
*/
public static void put(@NonNull final String key, final String value) {
put(key, value, getDefaultSpUtils());
}

/**
* 提交map,一次性commit,减少频繁读写IO
*
* @param hashmap 存储集合
*/
public static void put(@NotNull final Map hashmap) {
put(hashmap, getDefaultSpUtils());
}


/**
* Put the string value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public static void put(@NonNull final String key, final String value, final boolean isCommit) {
put(key, value, isCommit, getDefaultSpUtils());
}

/**
* 提交map,一次性commit,减少频繁读写IO
*
* @param hashmap 存储集合
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public static void put(final Map hashmap, final boolean isCommit) {
put(hashmap, isCommit, getDefaultSpUtils());
}


/**
* Return the string value in sp.
*
* @param key The key of sp.
* @return the string value if sp exists or {@code ""} otherwise
*/
public static String getString(@NonNull final String key) {
return getString(key, getDefaultSpUtils());
}

/**
* Return the string value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn"t exist.
* @return the string value if sp exists or {@code defaultValue} otherwise
*/
public static String getString(@NonNull final String key, final String defaultValue) {
return getString(key, defaultValue, getDefaultSpUtils());
}


/**
* Put the int value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
*/
public static void put(@NonNull final String key, final int value) {
put(key, value, getDefaultSpUtils());
}

/**
* Put the int value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public static void put(@NonNull final String key, final int value, final boolean isCommit) {
put(key, value, isCommit, getDefaultSpUtils());
}

/**
* Return the int value in sp.
*
* @param key The key of sp.
* @return the int value if sp exists or {@code -1} otherwise
*/
public static int getInt(@NonNull final String key) {
return getInt(key, getDefaultSpUtils());
}

/**
* Return the int value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn"t exist.
* @return the int value if sp exists or {@code defaultValue} otherwise
*/
public static int getInt(@NonNull final String key, final int defaultValue) {
return getInt(key, defaultValue, getDefaultSpUtils());
}

/**
* Put the long value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
*/
public static void put(@NonNull final String key, final long value) {
put(key, value, getDefaultSpUtils());
}

/**
* Put the long value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public static void put(@NonNull final String key, final long value, final boolean isCommit) {
put(key, value, isCommit, getDefaultSpUtils());
}

/**
* Return the long value in sp.
*
* @param key The key of sp.
* @return the long value if sp exists or {@code -1} otherwise
*/
public static long getLong(@NonNull final String key) {
return getLong(key, getDefaultSpUtils());
}

/**
* Return the long value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn"t exist.
* @return the long value if sp exists or {@code defaultValue} otherwise
*/
public static long getLong(@NonNull final String key, final long defaultValue) {
return getLong(key, defaultValue, getDefaultSpUtils());
}

/**
* Put the float value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
*/
public static void put(@NonNull final String key, final float value) {
put(key, value, getDefaultSpUtils());
}

/**
* Put the float value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public static void put(@NonNull final String key, final float value, final boolean isCommit) {
put(key, value, isCommit, getDefaultSpUtils());
}

/**
* Return the float value in sp.
*
* @param key The key of sp.
* @return the float value if sp exists or {@code -1f} otherwise
*/
public static float getFloat(@NonNull final String key) {
return getFloat(key, getDefaultSpUtils());
}

/**
* Return the float value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn"t exist.
* @return the float value if sp exists or {@code defaultValue} otherwise
*/
public static float getFloat(@NonNull final String key, final float defaultValue) {
return getFloat(key, defaultValue, getDefaultSpUtils());
}

/**
* Put the boolean value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
*/
public static void put(@NonNull final String key, final boolean value) {
put(key, value, getDefaultSpUtils());
}

/**
* Put the boolean value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public static void put(@NonNull final String key, final boolean value, final boolean isCommit) {
put(key, value, isCommit, getDefaultSpUtils());
}

/**
* Return the boolean value in sp.
*
* @param key The key of sp.
* @return the boolean value if sp exists or {@code false} otherwise
*/
public static boolean getBoolean(@NonNull final String key) {
return getBoolean(key, getDefaultSpUtils());
}

/**
* Return the boolean value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn"t exist.
* @return the boolean value if sp exists or {@code defaultValue} otherwise
*/
public static boolean getBoolean(@NonNull final String key, final boolean defaultValue) {
return getBoolean(key, defaultValue, getDefaultSpUtils());
}

/**
* Put the set of string value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
*/
public static void put(@NonNull final String key, final Set value) {
put(key, value, getDefaultSpUtils());
}

/**
* Put the set of string value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public static void put(@NonNull final String key,
final Set value,
final boolean isCommit)
{
put(key, value, isCommit, getDefaultSpUtils());
}

/**
* Return the set of string value in sp.
*
* @param key The key of sp.
* @return the set of string value if sp exists
* or {@code Collections.emptySet()} otherwise
*/

public static Set getStringSet(@NonNull final String key) {
return getStringSet(key, getDefaultSpUtils());
}

/**
* Return the set of string value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn"t exist.
* @return the set of string value if sp exists or {@code defaultValue} otherwise
*/
public static Set getStringSet(@NonNull final String key,
final Set defaultValue)
{
return getStringSet(key, defaultValue, getDefaultSpUtils());
}

/**
* Return all values in sp.
*
* @return all values in sp
*/
public static Map getAll() {
return getAll(getDefaultSpUtils());
}

/**
* Return whether the sp contains the preference.
*
* @param key The key of sp.
* @return {@code true}: yes
{@code false}: no
*/

public static boolean contains(@NonNull final String key) {
return contains(key, getDefaultSpUtils());
}

/**
* Remove the preference in sp.
*
* @param key The key of sp.
*/
public static void remove(@NonNull final String key) {
remove(key, getDefaultSpUtils());
}

/**
* Remove the preference in sp.
*
* @param key The key of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public static void remove(@NonNull final String key, final boolean isCommit) {
remove(key, isCommit, getDefaultSpUtils());
}

/**
* Remove all preferences in sp.
*/
public static void clear() {
clear(getDefaultSpUtils());
}

/**
* Remove all preferences in sp.
*
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public static void clear(final boolean isCommit) {
clear(isCommit, getDefaultSpUtils());
}

///////////////////////////////////////////////////////////////////////////
// dividing line
///////////////////////////////////////////////////////////////////////////

/**
* Put the string value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param spUtils The instance of {@link SpUtils}.
*/
public static void put(@NonNull final String key, final String value, @NonNull final SpUtils spUtils) {
spUtils.put(key, value);
}

/**
* 一次性提交
*
* @param map 存储集合
* @param spUtils The instance of {@link SpUtils}.
*/
public static void put(@NonNull final Map map, @NonNull final SpUtils spUtils) {
spUtils.put(map);
}

/**
* Put the string value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
* @param spUtils The instance of {@link SpUtils}.
*/
public static void put(@NonNull final String key,
final String value,
final boolean isCommit,
@NonNull final SpUtils spUtils) {
spUtils.put(key, value, isCommit);
}

/**
* 一次性提交
*
* @param map 存储集合
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
* @param spUtils The instance of {@link SpUtils}.
*/
public static void put(@NonNull final Map map, final boolean isCommit, @NonNull final SpUtils spUtils) {
spUtils.put(map, isCommit);
}

/**
* Return the string value in sp.
*
* @param key The key of sp.
* @param spUtils The instance of {@link SpUtils}.
* @return the string value if sp exists or {@code ""} otherwise
*/
public static String getString(@NonNull final String key, @NonNull final SpUtils spUtils) {
return spUtils.getString(key);
}

/**
* Return the string value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn"t exist.
* @param spUtils The instance of {@link SpUtils}.
* @return the string value if sp exists or {@code defaultValue} otherwise
*/
public static String getString(@NonNull final String key,
final String defaultValue,
@NonNull final SpUtils spUtils) {
return spUtils.getString(key, defaultValue);
}


/**
* Put the int value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param spUtils The instance of {@link SpUtils}.
*/
public static void put(@NonNull final String key, final int value, @NonNull final SpUtils spUtils) {
spUtils.put(key, value);
}

/**
* Put the int value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
* @param spUtils The instance of {@link SpUtils}.
*/
public static void put(@NonNull final String key,
final int value,
final boolean isCommit,
@NonNull final SpUtils spUtils) {
spUtils.put(key, value, isCommit);
}

/**
* Return the int value in sp.
*
* @param key The key of sp.
* @param spUtils The instance of {@link SpUtils}.
* @return the int value if sp exists or {@code -1} otherwise
*/
public static int getInt(@NonNull final String key, @NonNull final SpUtils spUtils) {
return spUtils.getInt(key);
}

/**
* Return the int value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn"t exist.
* @param spUtils The instance of {@link SpUtils}.
* @return the int value if sp exists or {@code defaultValue} otherwise
*/
public static int getInt(@NonNull final String key, final int defaultValue, @NonNull final SpUtils spUtils) {
return spUtils.getInt(key, defaultValue);
}

/**
* Put the long value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param spUtils The instance of {@link SpUtils}.
*/
public static void put(@NonNull final String key, final long value, @NonNull final SpUtils spUtils) {
spUtils.put(key, value);
}

/**
* Put the long value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
* @param spUtils The instance of {@link SpUtils}.
*/
public static void put(@NonNull final String key,
final long value,
final boolean isCommit,
@NonNull final SpUtils spUtils) {
spUtils.put(key, value, isCommit);
}

/**
* Return the long value in sp.
*
* @param key The key of sp.
* @param spUtils The instance of {@link SpUtils}.
* @return the long value if sp exists or {@code -1} otherwise
*/
private static long getLong(@NonNull final String key, @NonNull final SpUtils spUtils) {
return spUtils.getLong(key);
}

/**
* Return the long value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn"t exist.
* @param spUtils The instance of {@link SpUtils}.
* @return the long value if sp exists or {@code defaultValue} otherwise
*/
private static long getLong(@NonNull final String key, final long defaultValue, @NonNull final SpUtils spUtils) {
return spUtils.getLong(key, defaultValue);
}

/**
* Put the float value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param spUtils The instance of {@link SpUtils}.
*/
public static void put(@NonNull final String key, final float value, @NonNull final SpUtils spUtils) {
spUtils.put(key, value);
}

/**
* Put the float value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
* @param spUtils The instance of {@link SpUtils}.
*/
public static void put(@NonNull final String key,
final float value,
final boolean isCommit,
@NonNull final SpUtils spUtils) {
spUtils.put(key, value, isCommit);
}

/**
* Return the float value in sp.
*
* @param key The key of sp.
* @param spUtils The instance of {@link SpUtils}.
* @return the float value if sp exists or {@code -1f} otherwise
*/
private static float getFloat(@NonNull final String key, @NonNull final SpUtils spUtils) {
return spUtils.getFloat(key);
}

/**
* Return the float value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn"t exist.
* @param spUtils The instance of {@link SpUtils}.
* @return the float value if sp exists or {@code defaultValue} otherwise
*/
private static float getFloat(@NonNull final String key, final float defaultValue, @NonNull final SpUtils spUtils) {
return spUtils.getFloat(key, defaultValue);
}

/**
* Put the boolean value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param spUtils The instance of {@link SpUtils}.
*/
public static void put(@NonNull final String key, final boolean value, @NonNull final SpUtils spUtils) {
spUtils.put(key, value);
}

/**
* Put the boolean value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
* @param spUtils The instance of {@link SpUtils}.
*/
public static void put(@NonNull final String key,
final boolean value,
final boolean isCommit,
@NonNull final SpUtils spUtils) {
spUtils.put(key, value, isCommit);
}

/**
* Return the boolean value in sp.
*
* @param key The key of sp.
* @param spUtils The instance of {@link SpUtils}.
* @return the boolean value if sp exists or {@code false} otherwise
*/
public static boolean getBoolean(@NonNull final String key, @NonNull final SpUtils spUtils) {
return spUtils.getBoolean(key);
}

/**
* Return the boolean value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn"t exist.
* @param spUtils The instance of {@link SpUtils}.
* @return the boolean value if sp exists or {@code defaultValue} otherwise
*/
public static boolean getBoolean(@NonNull final String key,
final boolean defaultValue,
@NonNull final SpUtils spUtils) {
return spUtils.getBoolean(key, defaultValue);
}

/**
* Put the set of string value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param spUtils The instance of {@link SpUtils}.
*/
public static void put(@NonNull final String key, final Set value, @NonNull final SpUtils spUtils) {
spUtils.put(key, value);
}

/**
* Put the set of string value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
* @param spUtils The instance of {@link SpUtils}.
*/
public static void put(@NonNull final String key,
final Set value,
final boolean isCommit,
@NonNull final SpUtils spUtils)
{
spUtils.put(key, value, isCommit);
}

/**
* Return the set of string value in sp.
*
* @param key The key of sp.
* @param spUtils The instance of {@link SpUtils}.
* @return the set of string value if sp exists
* or {@code Collections.emptySet()} otherwise
*/

private static Set getStringSet(@NonNull final String key, @NonNull final SpUtils spUtils) {
return spUtils.getStringSet(key);
}

/**
* Return the set of string value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn"t exist.
* @param spUtils The instance of {@link SpUtils}.
* @return the set of string value if sp exists or {@code defaultValue} otherwise
*/
private static Set getStringSet(@NonNull final String key,
final Set defaultValue,
@NonNull final SpUtils spUtils)
{
return spUtils.getStringSet(key, defaultValue);
}

/**
* Return all values in sp.
*
* @param spUtils The instance of {@link SpUtils}.
* @return all values in sp
*/
private static Map getAll(@NonNull final SpUtils spUtils) {
return spUtils.getAll();
}

/**
* Return whether the sp contains the preference.
*
* @param key The key of sp.
* @param spUtils The instance of {@link SpUtils}.
* @return {@code true}: yes
{@code false}: no
*/

public static boolean contains(@NonNull final String key, @NonNull final SpUtils spUtils) {
return spUtils.contains(key);
}

/**
* Remove the preference in sp.
*
* @param key The key of sp.
* @param spUtils The instance of {@link SpUtils}.
*/
public static void remove(@NonNull final String key, @NonNull final SpUtils spUtils) {
spUtils.remove(key);
}

/**
* Remove the preference in sp.
*
* @param key The key of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
* @param spUtils The instance of {@link SpUtils}.
*/
public static void remove(@NonNull final String key, final boolean isCommit, @NonNull final SpUtils spUtils) {
spUtils.remove(key, isCommit);
}

/**
* Remove all preferences in sp.
*
* @param spUtils The instance of {@link SpUtils}.
*/
public static void clear(@NonNull final SpUtils spUtils) {
spUtils.clear();
}

/**
* Remove all preferences in sp.
*
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
* @param spUtils The instance of {@link SpUtils}.
*/
public static void clear(final boolean isCommit, @NonNull final SpUtils spUtils) {
spUtils.clear(isCommit);
}

/**
* 获取简单存储工具类 SpUtils
*
* @return SpUtils
*/
private static SpUtils getDefaultSpUtils() {
return sDefaultSpUtils != null ? sDefaultSpUtils : SpUtils.getInstance(getApplicationByReflect());
}

/**
* 如果你没有初始化该工具类,那么我会通过反射获取当前的applicationContext()
*
* @return Application
*/
private static Application getApplicationByReflect() {
try {
@SuppressLint("PrivateApi")
Class activityThread = Class.forName("android.app.ActivityThread");
Object thread = activityThread.getMethod("currentActivityThread").invoke(null);
Object app = activityThread.getMethod("getApplication").invoke(thread);
if (app == null) {
throw new NullPointerException("u should init first");
}
return (Application) app;
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
throw new NullPointerException("u should init first");
}
}



public class SpUtils {
private static final Map SP_UTILS_MAP = new HashMap<>();
private SharedPreferences sp;

/**
* Return the single {@link SpUtils} instance
*
* @param context context
* @return the single {@link SpUtils} instance
*/
public static SpUtils getInstance(Context context) {
return getInstance(context, "", Context.MODE_PRIVATE);
}

/**
* Return the single {@link SpUtils} instance
*
* @param context context
* @param mode Operating mode.
* @return the single {@link SpUtils} instance
*/
public static SpUtils getInstance(Context context, final int mode) {
return getInstance(context, "", mode);
}

/**
* Return the single {@link SpUtils} instance
*
* @param context context
* @param spName The name of sp.
* @return the single {@link SpUtils} instance
*/
public static SpUtils getInstance(Context context, String spName) {
return getInstance(context, spName, Context.MODE_PRIVATE);
}

/**
* Return the single {@link SpUtils} instance
*
* @param context context
* @param spName The name of sp.
* @param mode Operating mode.
* @return the single {@link SpUtils} instance
*/
public static SpUtils getInstance(Context context, String spName, final int mode) {
if (context == null) {
throw new UnsupportedOperationException("context can"t empty, please init me in SpHelpUtils.class");
}
if (isSpace(spName)) {
spName = context.getPackageName() + "_preferences";
}
SpUtils spUtils = SP_UTILS_MAP.get(spName);
if (spUtils == null) {
synchronized (SpUtils.class) {
spUtils = SP_UTILS_MAP.get(spName);
if (spUtils == null) {
spUtils = new SpUtils(context, spName, mode);
SP_UTILS_MAP.put(spName, spUtils);
}
}
}
return spUtils;
}

private SpUtils(Context context, final String spName) {
sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
}

private SpUtils(final Context context, final String spName, final int mode) {
sp = context.getSharedPreferences(spName, mode);
}

/**
* Put the string value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
*/
public void put(@NonNull final String key, final String value) {
// TODO 由于项目的功能需求,这个地方标志位统一改为走commit方式,后期需要逐一优化成apply
put(key, value, true);
}

/**
* 提交Map结合,用于一次性的commit或者apply,减少平凡读写IO
*
* @param map 存储集合
*/
public void put(@NonNull final Map map) {
put(map, true);
}

/**
* Put the string value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public void put(@NonNull final String key, final String value, final boolean isCommit) {
if (isCommit) {
sp.edit().putString(key, value).commit();
} else {
sp.edit().putString(key, value).apply();
}
}

/**
* @param map 存储集合
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public void put(@NonNull final Map map, final boolean isCommit) {
SharedPreferences.Editor edit = sp.edit();
for (Map.Entry next : map.entrySet()) {
if (next.getValue() instanceof String) {
edit.putString(next.getKey(), String.valueOf(next.getValue()));
} else if (next.getValue() instanceof Boolean) {
edit.putBoolean(next.getKey(), (Boolean) next.getValue());
} else if (next.getValue() instanceof Integer) {
edit.putInt(next.getKey(), (Integer) next.getValue());
} else if (next.getValue() instanceof Float) {
edit.putFloat(next.getKey(), (Float) next.getValue());
} else if (next.getValue() instanceof Long) {
edit.putLong(next.getKey(), (Long) next.getValue());
} else {
throw new UnsupportedOperationException("parameter Unsupported type!");
}
}
if (isCommit) {
edit.commit();
} else {
edit.apply();
}
}

/**
* Return the string value in sp.
*
* @param key The key of sp.
* @return the string value if sp exists or {@code ""} otherwise
*/
public String getString(@NonNull final String key) {
return getString(key, "");
}

/**
* Return the string value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn"t exist.
* @return the string value if sp exists or {@code defaultValue} otherwise
*/
public String getString(@NonNull final String key, final String defaultValue) {
return sp.getString(key, defaultValue);
}

/**
* Put the int value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
*/
public void put(@NonNull final String key, final int value) {
put(key, value, false);
}

/**
* Put the int value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public void put(@NonNull final String key, final int value, final boolean isCommit) {
if (isCommit) {
sp.edit().putInt(key, value).commit();
} else {
sp.edit().putInt(key, value).apply();
}
}

/**
* Return the int value in sp.
*
* @param key The key of sp.
* @return the int value if sp exists or {@code -1} otherwise
*/
public int getInt(@NonNull final String key) {
return getInt(key, -1);
}

/**
* Return the int value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn"t exist.
* @return the int value if sp exists or {@code defaultValue} otherwise
*/
public int getInt(@NonNull final String key, final int defaultValue) {
return sp.getInt(key, defaultValue);
}

/**
* Put the long value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
*/
public void put(@NonNull final String key, final long value) {
put(key, value, false);
}

/**
* Put the long value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public void put(@NonNull final String key, final long value, final boolean isCommit) {
if (isCommit) {
sp.edit().putLong(key, value).commit();
} else {
sp.edit().putLong(key, value).apply();
}
}

/**
* Return the long value in sp.
*
* @param key The key of sp.
* @return the long value if sp exists or {@code -1} otherwise
*/
public long getLong(@NonNull final String key) {
return getLong(key, -1L);
}

/**
* Return the long value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn"t exist.
* @return the long value if sp exists or {@code defaultValue} otherwise
*/
public long getLong(@NonNull final String key, final long defaultValue) {
return sp.getLong(key, defaultValue);
}

/**
* Put the float value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
*/
public void put(@NonNull final String key, final float value) {
put(key, value, false);
}

/**
* Put the float value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public void put(@NonNull final String key, final float value, final boolean isCommit) {
if (isCommit) {
sp.edit().putFloat(key, value).commit();
} else {
sp.edit().putFloat(key, value).apply();
}
}

/**
* Return the float value in sp.
*
* @param key The key of sp.
* @return the float value if sp exists or {@code -1f} otherwise
*/
public float getFloat(@NonNull final String key) {
return getFloat(key, -1f);
}

/**
* Return the float value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn"t exist.
* @return the float value if sp exists or {@code defaultValue} otherwise
*/
public float getFloat(@NonNull final String key, final float defaultValue) {
return sp.getFloat(key, defaultValue);
}

/**
* Put the boolean value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
*/
public void put(@NonNull final String key, final boolean value) {
put(key, value, false);
}

/**
* Put the boolean value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public void put(@NonNull final String key, final boolean value, final boolean isCommit) {
if (isCommit) {
sp.edit().putBoolean(key, value).commit();
} else {
sp.edit().putBoolean(key, value).apply();
}
}

/**
* Return the boolean value in sp.
*
* @param key The key of sp.
* @return the boolean value if sp exists or {@code false} otherwise
*/
public boolean getBoolean(@NonNull final String key) {
return getBoolean(key, false);
}

/**
* Return the boolean value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn"t exist.
* @return the boolean value if sp exists or {@code defaultValue} otherwise
*/
public boolean getBoolean(@NonNull final String key, final boolean defaultValue) {
return sp.getBoolean(key, defaultValue);
}

/**
* Put the set of string value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
*/
public void put(@NonNull final String key, final Set value) {
put(key, value, false);
}

/**
* Put the set of string value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public void put(@NonNull final String key,
final Set value,
final boolean isCommit)
{
if (isCommit) {
sp.edit().putStringSet(key, value).commit();
} else {
sp.edit().putStringSet(key, value).apply();
}
}

/**
* Return the set of string value in sp.
*
* @param key The key of sp.
* @return the set of string value if sp exists
* or {@code Collections.emptySet()} otherwise
*/

public Set getStringSet(@NonNull final String key) {
return getStringSet(key, Collections.emptySet());
}

/**
* Return the set of string value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn"t exist.
* @return the set of string value if sp exists or {@code defaultValue} otherwise
*/
public Set getStringSet(@NonNull final String key,
final Set defaultValue)
{
return sp.getStringSet(key, defaultValue);
}

/**
* Return all values in sp.
*
* @return all values in sp
*/
public Map getAll() {
return sp.getAll();
}

/**
* Return whether the sp contains the preference.
*
* @param key The key of sp.
* @return {@code true}: yes
{@code false}: no
*/

public boolean contains(@NonNull final String key) {
return sp.contains(key);
}

/**
* Remove the preference in sp.
*
* @param key The key of sp.
*/
public void remove(@NonNull final String key) {
remove(key, false);
}

/**
* Remove the preference in sp.
*
* @param key The key of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public void remove(@NonNull final String key, final boolean isCommit) {
if (isCommit) {
sp.edit().remove(key).commit();
} else {
sp.edit().remove(key).apply();
}
}

/**
* Remove all preferences in sp.
*/
public void clear() {
clear(false);
}

/**
* Remove all preferences in sp.
*
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public void clear(final boolean isCommit) {
if (isCommit) {
sp.edit().clear().commit();
} else {
sp.edit().clear().apply();
}
}

private static boolean isSpace(final String s) {
if (s == null) {
return true;
}
for (int i = 0, len = s.length(); i < len; ++i) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}
return true;
}
}

感谢各位大佬的阅读,如果存在不足的地方或者有更好的建议,请留言回复,我会尽量把文章的质量做到更好以分享给更多的读者,谢谢!

—————END—————

? ? ?

? ?创作不易,点个“在看”

上帝之手

赞赏

长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

相关文章:
上一集:没有了 下一集:DIY端午小粽子