ShareUtil进化版(支持并发)
Posted 钰娘娘ynn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ShareUtil进化版(支持并发)相关的知识,希望对你有一定的参考价值。
此处使用itemShares保存不同的share,防止因并发调用,导致数据混乱。
public class ShareUtils
private static String DEFAULT_NAME = "default";
private String shareName = DEFAULT_NAME;
private int shareMode = Context.MODE_PRIVATE;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
private static ShareUtils outterShare;
private static List<ShareUtils> itemShares = new ArrayList<>();
private ShareUtils()
public static ShareUtils getInstance()
if(outterShare == null)
outterShare = new ShareUtils();
return outterShare;
private SharedPreferences getShare()
if (sharedPreferences == null)
sharedPreferences = MyApp.getApp().getSharedPreferences(shareName, shareMode);
return sharedPreferences;
public ShareUtils resetShare()
commit();
if (!this.shareName.equals(DEFAULT_NAME) || this.shareMode != Context.MODE_PRIVATE)
sharedPreferences = null;
editor = null;
this.shareName = DEFAULT_NAME;
this.shareMode = Context.MODE_PRIVATE;
for(int i = itemShares.size()-1; i >=0; i--)
if(itemShares.get(i) == null)
itemShares.remove(i);
continue;
if(itemShares.get(i).shareName.equals(DEFAULT_NAME)
&& itemShares.get(i).shareMode ==Context.MODE_PRIVATE)
return itemShares.get(i);
ShareUtils innerUtils = new ShareUtils();
innerUtils.shareMode = Context.MODE_PRIVATE;
innerUtils.shareName = DEFAULT_NAME;
itemShares.add(innerUtils);
return innerUtils;
/**
* ShareUtils在同一个类中要调用不同share,进行重置
*
* @param shareName
* @return
public ShareUtils resetShare(String shareName)
commit();
if (!this.shareName.equals(shareName) || this.shareMode != Context.MODE_PRIVATE)
sharedPreferences = null;
editor = null;
this.shareName = shareName;
this.shareMode = Context.MODE_PRIVATE;
for(int i = itemShares.size()-1; i >=0; i--)
if(itemShares.get(i) == null)
itemShares.remove(i);
continue;
if(itemShares.get(i).shareName.equals(shareName)
&& itemShares.get(i).shareMode ==Context.MODE_PRIVATE)
return itemShares.get(i);
ShareUtils innerUtils = new ShareUtils();
innerUtils.shareMode = Context.MODE_PRIVATE;
innerUtils.shareName = shareName;
itemShares.add(innerUtils);
return innerUtils;
/**
* ShareUtils在同一个类中要调用不同share,进行重置
*
* @param shareName
* @return
public ShareUtils resetShare(String shareName, int shareMode)
commit();
if (!this.shareName.equals(shareName) || this.shareMode != shareMode)
sharedPreferences = null;
editor = null;
this.shareName = shareName;
this.shareMode = shareMode;
this.shareName = shareName;
this.shareMode = Context.MODE_PRIVATE;
for(int i = itemShares.size()-1; i >=0; i--)
if(itemShares.get(i) == null)
itemShares.remove(i);
continue;
if(itemShares.get(i).shareName.equals(shareName)
&& itemShares.get(i).shareMode ==shareMode)
return itemShares.get(i);
ShareUtils innerUtils = new ShareUtils();
innerUtils.shareMode = shareMode;
innerUtils.shareName = shareName;
itemShares.add(innerUtils);
return innerUtils;
private SharedPreferences.Editor getEditor()
if (editor == null)
editor = getShare().edit();
return editor;
public ShareUtils set(String name, int intValue)
getEditor().putInt(name, intValue);
return this;
public ShareUtils set(String name, long longValue)
getEditor().putLong(name, longValue);
return this;
public ShareUtils set(String name, boolean boolValue)
getEditor().putBoolean(name, boolValue);
return this;
public ShareUtils set(String name, float floatValue)
getEditor().putFloat(name, floatValue);
return this;
public ShareUtils set(String name, String stringValue)
getEditor().putString(name, stringValue);
return this;
public ShareUtils set(String name, Set<String> stringSet)
getEditor().putStringSet(name, stringSet);
return this;
public void commit()
if (editor != null)
editor.apply();
public void clear()
getEditor().clear().apply();
public void remove(String name)
getEditor().remove(name).apply();
public int getInt(String name)
return getShare().getInt(name, 0);
public int getInt(String name, int defaultValue)
return getShare().getInt(name, defaultValue);
public long getLong(String name)
return getShare().getLong(name, 0);
public long getLong(String name, long defaultValue)
return getShare().getLong(name, defaultValue);
public boolean getBoolean(String name)
return getShare().getBoolean(name, false);
public boolean getBoolean(String name, boolean defaultValue)
return getShare().getBoolean(name, defaultValue);
public float getFloat(String name)
return getShare().getFloat(name, 0f);
public float getFloat(String name, float defaultValue)
return getShare().getFloat(name, defaultValue);
public String getString(String name)
return getShare().getString(name, "");
public String getString(String name, String defaultValue)
return getShare().getString(name, defaultValue);
public Set<String> getSet(String name)
return getShare().getStringSet(name, null);
public Set<String> getSet(String name, Set<String> defaultValue)
return
以上是关于ShareUtil进化版(支持并发)的主要内容,如果未能解决你的问题,请参考以下文章