Created
September 29, 2015 03:25
-
-
Save leasual/275073986f6451445b21 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.SharedPreferences; | |
/** | |
* Created by james.li on 2015/9/29. | |
*/ | |
public class SPreferenceUtil { | |
private static SPreferenceUtil instance; | |
private SharedPreferences mSharedPreferences; | |
private SharedPreferences.Editor editor; | |
private SPreferenceUtil(Context context,String preferenceName){ | |
mSharedPreferences = context.getSharedPreferences(preferenceName,Context.MODE_PRIVATE); | |
editor = mSharedPreferences.edit(); | |
} | |
public static SPreferenceUtil getInstance(Context context,String preferenceName){ | |
if (instance == null){ | |
synchronized (SPreferenceUtil.class){ | |
if (instance == null){ | |
//使用双重同步锁 | |
instance = new SPreferenceUtil(context,preferenceName); | |
} | |
} | |
} | |
return instance; | |
} | |
public SPreferenceUtil getInstance(Context context){ | |
return getInstance(context,context.getPackageName() + "_preferences"); | |
} | |
/** | |
* 得到SharedPreferences.Editor,如果之后的操作强烈依赖于存入的数据 | |
* 那么请用editor的commit方法进行提交。 | |
*/ | |
public SharedPreferences.Editor getEditor() { | |
return editor; | |
} | |
public void putStringByApply(String key, String value) { | |
editor.putString(key, value).apply(); | |
} | |
public void putStringByCommit(String key, String value) { | |
editor.putString(key, value).commit(); | |
} | |
public String getString(String key, String defValue) { | |
return mSharedPreferences.getString(key, defValue); | |
} | |
public void putIntByApply(String key, int value) { | |
editor.putInt(key, value).apply(); | |
} | |
public void putIntByCommit(String key, int value) { | |
editor.putInt(key, value).commit(); | |
} | |
public int getInt(String key, int defValue) { | |
return mSharedPreferences.getInt(key, defValue); | |
} | |
public void putBooleanByApply(String key, boolean value) { | |
editor.putBoolean(key, value).apply(); | |
} | |
public void putBooleanByCommit(String key, boolean value) { | |
editor.putBoolean(key, value).commit(); | |
} | |
public boolean getBoolean(String key, boolean defValue) { | |
return mSharedPreferences.getBoolean(key, defValue); | |
} | |
public void putLongByApply(String key, long value) { | |
editor.putLong(key, value).apply(); | |
} | |
public void putLongByCommit(String key, long value) { | |
editor.putLong(key, value).commit(); | |
} | |
public long getLong(String key, long defValue) { | |
return mSharedPreferences.getLong(key, defValue); | |
} | |
public void removeByApply(String key) { | |
editor.remove(key).apply(); | |
} | |
public void removeByCommit(String key) { | |
editor.remove(key).commit(); | |
} | |
public void clearByApply() { | |
editor.clear().apply(); | |
} | |
public void clearByCommit() { | |
editor.clear().commit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment