how to create a session for remember me (login authentication) in android

Last Updated:

  1. Amalthomas

    Amalthomas Member

    I have developed a code for login/register authentication. I want to add the remember me feature in my app. I am new to android.i want to know clearly about session for remember me. can you guide me right direction...?
    Here is my code for login:

    public class LoginActivity extends Activity {
    Button btnLogin;
    Button btnLinkToRegister;
    EditText inputEmail;
    EditText inputPassword;
    TextView loginErrorMsg;
    //CheckBox chkRemember;
    // JSON Response node names
    private static String KEY_SUCCESS = "success";
    private static String KEY_UID = "uid";
    private static String KEY_NAME = "name";
    private static String KEY_EMAIL = "email";
    private static String KEY_CREATED_AT = "created_at";

    public void onCreate(Bundle savedInstanceState) {

    // Importing all assets like buttons, text fields
    inputEmail = (EditText) findViewById(;
    inputPassword = (EditText) findViewById(;
    btnLogin = (Button) findViewById(;
    btnLinkToRegister = (Button) findViewById(;
    //chkRemember =(CheckBox)findViewById(;
    loginErrorMsg = (TextView) findViewById(;

    // Login button Click Event
    btnLogin.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {
    String email = inputEmail.getText().toString();
    String password = inputPassword.getText().toString();
    UserFunctions userFunction = new UserFunctions();
    Log.d("Button", "Login");
    JSONObject json = userFunction.loginUser(email, password);

    // check for login response
    try {
    if (json.getString(KEY_SUCCESS) != null) {
    String res = json.getString(KEY_SUCCESS);
    if(Integer.parseInt(res) == 1){
    // user successfully logged in
    // Store user details in SQLite Database
    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
    JSONObject json_user = json.getJSONObject("user");

    // Clear all previous data in database
    db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));

    // Launch Dashboard Screen
    Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);

    // Close all views before launching Dashboard

    // Close Login Screen
    // Error in login
    loginErrorMsg.setText("Incorrect username/password");
    } catch (JSONException e) {

    // Link to Register Screen
    btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {
    Intent i = new Intent(getApplicationContext(),

  2. ayazshakeeb

    ayazshakeeb New Member

    use sharedPreference Tutorial u'll get the answer
  3. Amalthomas

    Amalthomas Member

    @ayazshakeeb, can u guide me to use sharedPreference. i am new to android.
  4. iowabowtech

    iowabowtech root@android:/ #

    Thread moved to the App Development forum. Hopefully you can find the answer you seek here Amalthomas. :)

  5. shredcode

    shredcode Active Member

    I agree, sharedPreferences is the answer to your problem. Here is a link to the api docs: link

    Here is also an easy code sample for you to refer to for setting and retrieving a String preference:

    public void setPassword(Context c, String password) {
    SharedPreferences settings = c.getSharedPreferences(YOUR_TAG, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("appPassword", password);

    public String getPassword(Context c) {
    SharedPreferences settings = c.getSharedPreferences(YOUR_TAG, Context.MODE_PRIVATE);
    String password = settings.getString("appPassword", "");
    return password;
  6. Amalthomas

    Amalthomas Member

    @shredcode, Thank you
  7. Amalthomas

    Amalthomas Member

    i wrote the above code for login/registration, i store all my details like name, email , password in MySQL by using PHP. i want to add "remember me" feature in my code. Any one help me to add remember me (my email and password) when i enable the checkbox. but here want to retrieve the data from MySQL. guide me. thanks in advance
  8. shredcode

    shredcode Active Member

    What is your table structure? Are you asking for the SQL / php code to query the db?

Share This Page