android sharedpreferences 工具类,Android 线程安全的SharedPreferences操作工具类

package com.mytian.utils;

import android.content.Context;

import android.content.SharedPreferences;

import android.os.AsyncTask;

import android.text.TextUtils;

import java.lang.ref.WeakReference;

import java.util.HashSet;

import java.util.Iterator;

import java.util.LinkedList;

import java.util.List;

import java.util.Map;

import java.util.Set;

public class SPUtils {

private static SharedPreferences sharedPreferences;

private static List>

onSharedPreferenceChangeListenerWeakList = new LinkedList<>();

private SPUtils(){

throw new AssertionError();

}

public static void init(Context context){

createSP(context);

}

private static void createSP(final Context context){

if(null == sharedPreferences) {

synchronized (SPUtils.class) {

if (null == sharedPreferences) {

sharedPreferences = context

.getSharedPreferences(context.getPackageName()

, Context.MODE_PRIVATE);

sharedPreferences.registerOnSharedPreferenceChangeListener((sharedPreferences, key) -> {

synchronized (SPUtils.class){

final Iterator> iterator =

onSharedPreferenceChangeListenerWeakList.iterator();

while (iterator.hasNext()){

final SharedPreferences.OnSharedPreferenceChangeListener listener

= iterator.next().get();

if(null == listener){

iterator.remove();

} else {

listener.onSharedPreferenceChanged(sharedPreferences,key);

}

}

}

});

AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> getAll(context));

}

}

}

}

public static String getString(final Context context,final String key

,final String defValue){

createSP(context);

synchronized (SPUtils.class){

return sharedPreferences.getString(key,defValue);

}

}

public static String getString(final Context context,final String key){

return getString(context,key,"");

}

public static void putString(final Context context,final String key

,final String value){

createSP(context);

synchronized (SPUtils.class){

sharedPreferences.edit().putString(key,value)

.commit();

}

}

public static boolean getBoolean(final Context context,final String key

,final boolean defValue){

createSP(context);

synchronized (SPUtils.class){

return sharedPreferences.getBoolean(key,defValue);

}

}

public static boolean getBoolean(final Context context,final String key){

return getBoolean(context,key,false);

}

public static void putBoolean(final Context context,final String key

,final boolean value){

createSP(context);

synchronized (SPUtils.class){

sharedPreferences.edit().putBoolean(key,value)

.commit();

}

}

public static float getFloat(final Context context,final String key

,final float defValue){

createSP(context);

synchronized (SPUtils.class){

return sharedPreferences.getFloat(key,defValue);

}

}

public static float getFloat(final Context context,final String key){

return getFloat(context,key,.0f);

}

public static void putFloat(final Context context,final String key

,final float value){

createSP(context);

synchronized (SPUtils.class){

sharedPreferences.edit().putFloat(key,value)

.commit();

}

}

public static int getInt(final Context context,final String key

,final int defValue){

createSP(context);

synchronized (SPUtils.class){

return sharedPreferences.getInt(key,defValue);

}

}

public static float getInt(final Context context,final String key){

return getInt(context,key,0);

}

public static void putInt(final Context context,final String key

,final int value){

createSP(context);

synchronized (SPUtils.class){

sharedPreferences.edit().putInt(key,value)

.commit();

}

}

public static long getLong(final Context context,final String key

,final long defValue){

createSP(context);

synchronized (SPUtils.class){

return sharedPreferences.getLong(key,defValue);

}

}

public static long getLong(final Context context,final String key){

return getLong(context,key,0);

}

public static void putLong(final Context context,final String key

,final long value){

createSP(context);

synchronized (SPUtils.class){

sharedPreferences.edit().putLong(key,value)

.commit();

}

}

public static Map getAll(final Context context){

createSP(context);

synchronized (SPUtils.class){

return sharedPreferences.getAll();

}

}

public static Set getStringSet(final Context context, String key){

createSP(context);

synchronized (SPUtils.class){

final HashSet set = new HashSet<>();

return sharedPreferences.getStringSet(key,set);

}

}

public static void putStringSet(final Context context,final String key

,final Set value){

createSP(context);

synchronized (SPUtils.class){

sharedPreferences.edit().putStringSet(key,value)

.commit();

}

}

public static void remove(final Context context,final String key){

createSP(context);

synchronized (SPUtils.class){

sharedPreferences.edit().remove(key)

.commit();

}

}

public static void registerOnSharedPreferenceChangeListener(

final Context context

, final SharedPreferences.OnSharedPreferenceChangeListener listener){

createSP(context);

synchronized (SPUtils.class){

onSharedPreferenceChangeListenerWeakList

.add(new WeakReference<>(listener));

}

}

public static void unregisterOnSharedPreferenceChangeListener(

final SharedPreferences.OnSharedPreferenceChangeListener listener){

if(null != listener){

synchronized (SPUtils.class){

final Iterator> iterator

= onSharedPreferenceChangeListenerWeakList.iterator();

while (iterator.hasNext()){

final SharedPreferences.OnSharedPreferenceChangeListener item

= iterator.next().get();

if(null == item || item.equals(listener)){

iterator.remove();

}

}

}

}

}

public static boolean contains(final Context context, final String key){

createSP(context);

synchronized (SPUtils.class){

return !TextUtils.isEmpty(key)

&& sharedPreferences.contains(key);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值