Skip to content

Instantly share code, notes, and snippets.

@WSAyan
Last active July 18, 2017 05:32
Show Gist options
  • Save WSAyan/0bc8658532caa73f2c355809970ea60b to your computer and use it in GitHub Desktop.
Save WSAyan/0bc8658532caa73f2c355809970ea60b to your computer and use it in GitHub Desktop.
Android SharedPreferences helper class. (EX: Log In session)
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
/**
* Created by WS Ayan on 11/24/2015.
*/
public class SessionManager {
private static String LOG = SessionManager.class.getSimpleName();
// Shared Preferences
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
// Shared pref mode
private static final int PRIVATE_MODE = 0;
// Shared preferences file name
private static final String PREF_NAME = "NAME";
// Key for storing
private static final String KEY_LOG_IN = "isLoggedIn";
public SessionManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void setLogin(boolean isLoggedIn) {
editor.putBoolean(KEY_LOGGED_IN, isLoggedIn);
// commit changes
editor.commit();
//Log.d(LOG, "User login session modified!");
}
public boolean isLoggedIn(){
return pref.getBoolean(KEY_LOG_IN, false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment