Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PreferencesHelper {
- private static final String TAG = "PreferencesHelper";
- private Context mApplicationContext;
- private static PreferencesHelper sInstance;
- private static final boolean PREFERENCES_ENABLED = true;
- private static SharedPreferences prefs;
- public static PreferencesHelper getInstance(Context context) {
- if (!PREFERENCES_ENABLED) {
- return sEmptyPreferencesHelper;
- }
- if (sInstance == null) {
- if (context == null) {
- return sEmptyPreferencesHelper;
- }
- sInstance = new PreferencesHelper(context);
- }
- return sInstance;
- }
- private PreferencesHelper(Context context) {
- if (context == null) {
- // This should only occur for the empty Preferences Helper object.
- return;
- }
- mApplicationContext = context.getApplicationContext();
- prefs = PreferenceManager.getDefaultSharedPreferences(mApplicationContext);
- Log.d(TAG, "Initializing Shared Preferences");
- }
- public String getValue(String key, String s) {
- return prefs.getString(key, s);
- }
- public void setValue(String key, String value) {
- prefs.edit().putString(key, value);
- }
- public int getValue(String key, int i) {
- return prefs.getInt(key, i);
- }
- public boolean getValue(String key, boolean b) {
- return prefs.getBoolean(key, b);
- }
- /**
- * Empty instance for use when Preferences is disabled or there was no Context available.
- */
- private static PreferencesHelper sEmptyPreferencesHelper = new PreferencesHelper(null) {
- @Override
- public String getValue(String key, String s) { return s; }
- @Override
- public void setValue(String key, String value) {}
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement