Skip to content

Instantly share code, notes, and snippets.

@kalysr
Last active November 24, 2018 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalysr/5dcd1e745d083a54da6b9d8701f6842d to your computer and use it in GitHub Desktop.
Save kalysr/5dcd1e745d083a54da6b9d8701f6842d to your computer and use it in GitHub Desktop.
public class SharedPreferenceHelper {
private final static String PREF_FILE = "PREF";
/**
* Set a string shared preference
* @param key - Key to set shared preference
* @param value - Value for the key
*/
static void setSharedPreferenceString(Context context, String key, String value){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
editor.apply();
}
/**
* Set a integer shared preference
* @param key - Key to set shared preference
* @param value - Value for the key
*/
static void setSharedPreferenceInt(Context context, String key, int value){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(key, value);
editor.apply();
}
/**
* Set a Boolean shared preference
* @param key - Key to set shared preference
* @param value - Value for the key
*/
static void setSharedPreferenceBoolean(Context context, String key, boolean value){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(key, value);
editor.apply();
}
/**
* Get a string shared preference
* @param key - Key to look up in shared preferences.
* @param defValue - Default value to be returned if shared preference isn't found.
* @return value - String containing value of the shared preference if found.
*/
static String getSharedPreferenceString(Context context, String key, String defValue){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
return settings.getString(key, defValue);
}
/**
* Get a integer shared preference
* @param key - Key to look up in shared preferences.
* @param defValue - Default value to be returned if shared preference isn't found.
* @return value - String containing value of the shared preference if found.
*/
static int getSharedPreferenceInt(Context context, String key, int defValue){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
return settings.getInt(key, defValue);
}
/**
* Get a boolean shared preference
* @param key - Key to look up in shared preferences.
* @param defValue - Default value to be returned if shared preference isn't found.
* @return value - String containing value of the shared preference if found.
*/
static boolean getSharedPreferenceBoolean(Context context, String key, boolean defValue){
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
return settings.getBoolean(key, defValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment