Created
July 13, 2016 08:45
-
-
Save dnldsht/20406e4db3c1bae7efbb9244b57f6861 to your computer and use it in GitHub Desktop.
Shared Preferences Helper
This file contains hidden or 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
public class PreferenceUtil { | |
private static PreferenceUtil instance; | |
private SharedPreferences SP; | |
private PreferenceUtil(Context mContext) { | |
SP = PreferenceManager.getDefaultSharedPreferences(mContext); | |
} | |
public static PreferenceUtil getInstance(Context context) { | |
if (instance == null) { | |
synchronized (PreferenceUtil.class) { | |
if (instance == null) | |
instance = new PreferenceUtil(context); | |
} | |
} | |
return instance; | |
} | |
public SharedPreferences.Editor getEditor() { | |
return SP.edit(); | |
} | |
public void putString(String key, String value) { | |
getEditor().putString(key, value).commit(); | |
} | |
public String getString(String key, String defValue) { | |
return SP.getString(key, defValue); | |
} | |
public void putInt(String key, int value) { | |
getEditor().putInt(key, value).commit(); | |
} | |
public int getInt(String key, int defValue) { | |
return SP.getInt(key, defValue); | |
} | |
public void putBoolean(String key, boolean value) { | |
getEditor().putBoolean(key, value).commit(); | |
} | |
public boolean getBoolean(String key, boolean defValue) { | |
return SP.getBoolean(key, defValue); | |
} | |
public void remove(String key) { | |
getEditor().remove(key).commit(); | |
} | |
public void clearPreferences() { | |
getEditor().clear().commit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment