Skip to content

Instantly share code, notes, and snippets.

@iChintanSoni
Last active December 5, 2018 01:52
Show Gist options
  • Save iChintanSoni/f1767fbe68f7f897cfb3096e8cd83480 to your computer and use it in GitHub Desktop.
Save iChintanSoni/f1767fbe68f7f897cfb3096e8cd83480 to your computer and use it in GitHub Desktop.
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
Copy link

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

@rohan20
Copy link

rohan20 commented Mar 8, 2018

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

@valterh4ck3r
Copy link

Great work !

@jmlp1327
Copy link

jmlp1327 commented May 8, 2018

Thanks 👍

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