Skip to content

Instantly share code, notes, and snippets.

@jefferson
Created September 2, 2016 02:27
Show Gist options
  • Save jefferson/e6bbc0d4e4a28edcf63c2b44a4d086cf to your computer and use it in GitHub Desktop.
Save jefferson/e6bbc0d4e4a28edcf63c2b44a4d086cf to your computer and use it in GitHub Desktop.
A simple profile class for android using shared preferences.
public class Profile {
public static final String USER_NAME = "_username";
public static final String TOKEN_ENVITED = "_token_envited";
public static void write(final Context context, final String key, final String value){
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// Initialize SharedPreferences
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(context);
// Make a new preferences editor
SharedPreferences.Editor e = getPrefs.edit();
// Edit preference to make it false because we don't want this _rotationTo run again
e.putString(key, value);
// Apply changes
e.apply();
}
});
// Start the thread
t.start();
}
public static void write(final Context context, final String key, final boolean value){
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// Initialize SharedPreferences
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(context);
// Make a new preferences editor
SharedPreferences.Editor e = getPrefs.edit();
// Edit preference _rotationTo make it false because we don't want this _rotationTo run again
e.putBoolean(key, value);
// Apply changes
e.apply();
}
});
// Start the thread
t.start();
}
public static String getString(Context context, String key){
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(context);
return getPrefs.getString(key, "");
}
public static Boolean getBoolean(Context context, String key){
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(context);
return getPrefs.getBoolean(key, false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment