Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Utility class for working with SharedPreferences
import android.content.Context;
import android.content.SharedPreferences;
public class SharedPreferenceUtils {
private static SharedPreferenceUtils mSharedPreferenceUtils;
protected Context mContext;
private SharedPreferences mSharedPreferences;
private SharedPreferences.Editor mSharedPreferencesEditor;
private SharedPreferenceUtils(Context context) {
mContext = context;
mSharedPreferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
mSharedPreferencesEditor = mSharedPreferences.edit();
}
/**
* Creates single instance of SharedPreferenceUtils
*
* @param context context of Activity or Service
* @return Returns instance of SharedPreferenceUtils
*/
public static synchronized SharedPreferenceUtils getInstance(Context context) {
if (mSharedPreferenceUtils == null) {
mSharedPreferenceUtils = new SharedPreferenceUtils(context.getApplicationContext());
}
return mSharedPreferenceUtils;
}
/**
* Stores String value in preference
*
* @param key key of preference
* @param value value for that key
*/
public void setValue(String key, String value) {
mSharedPreferencesEditor.putString(key, value);
mSharedPreferencesEditor.commit();
}
/**
* Stores int value in preference
*
* @param key key of preference
* @param value value for that key
*/
public void setValue(String key, int value) {
mSharedPreferencesEditor.putInt(key, value);
mSharedPreferencesEditor.commit();
}
/**
* Stores Double value in String format in preference
*
* @param key key of preference
* @param value value for that key
*/
public void setValue(String key, double value) {
setValue(key, Double.toString(value));
}
/**
* Stores long value in preference
*
* @param key key of preference
* @param value value for that key
*/
public void setValue(String key, long value) {
mSharedPreferencesEditor.putLong(key, value);
mSharedPreferencesEditor.commit();
}
/**
* Stores boolean value in preference
*
* @param key key of preference
* @param value value for that key
*/
public void setValue(String key, boolean value) {
mSharedPreferencesEditor.putBoolean(key, value);
mSharedPreferencesEditor.commit();
}
/**
* Retrieves String value from preference
*
* @param key key of preference
* @param defaultValue default value if no key found
*/
public String getStringValue(String key, String defaultValue) {
return mSharedPreferences.getString(key, defaultValue);
}
/**
* Retrieves int value from preference
*
* @param key key of preference
* @param defaultValue default value if no key found
*/
public int getIntValue(String key, int defaultValue) {
return mSharedPreferences.getInt(key, defaultValue);
}
/**
* Retrieves long value from preference
*
* @param key key of preference
* @param defaultValue default value if no key found
*/
public long getLongValue(String key, long defaultValue) {
return mSharedPreferences.getLong(key, defaultValue);
}
/**
* Retrieves boolean value from preference
*
* @param keyFlag key of preference
* @param defaultValue default value if no key found
*/
public boolean getBoolanValue(String keyFlag, boolean defaultValue) {
return mSharedPreferences.getBoolean(keyFlag, defaultValue);
}
/**
* Removes key from preference
*
* @param key key of preference that is to be deleted
*/
public void removeKey(String key) {
if (mSharedPreferencesEditor != null) {
mSharedPreferencesEditor.remove(key);
mSharedPreferencesEditor.commit();
}
}
/**
* Clears all the preferences stored
*/
public void clear() {
mSharedPreferencesEditor.clear().commit();
}
}
@davidshare

This comment has been minimized.

Copy link

@davidshare davidshare commented Sep 16, 2017

Great work, but how do you call this in another file?

@rohan20

This comment has been minimized.

Copy link

@rohan20 rohan20 commented Mar 8, 2018

@davidshare Use SharedPreferenceUtils.getInstance().methodName();
where methodName() is any of the methods in this class.

@valterh4ck3r

This comment has been minimized.

Copy link

@valterh4ck3r valterh4ck3r commented Apr 7, 2018

Great work !

@jmlp1327

This comment has been minimized.

Copy link

@jmlp1327 jmlp1327 commented May 8, 2018

Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment