• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

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

Amalthomas

Newbie
Mar 18, 2013
10
0
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";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);

// Importing all assets like buttons, text fields
inputEmail = (EditText) findViewById(R.id.loginEmail);
inputPassword = (EditText) findViewById(R.id.loginPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
//chkRemember =(CheckBox)findViewById(R.id.checkBox1);
loginErrorMsg = (TextView) findViewById(R.id.login_error);

// 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) {
loginErrorMsg.setText("");
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
userFunction.logoutUser(getApplicationContext());
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
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);

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

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

public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
RegisterActivity.class);
startActivity(i);
finish();
}
});
}
 
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:

[HIGH]
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);
editor.commit();
}

public String getPassword(Context c) {
SharedPreferences settings = c.getSharedPreferences(YOUR_TAG, Context.MODE_PRIVATE);
String password = settings.getString("appPassword", "");
return password;
}
[/HIGH]
 
Upvote 0
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
.....
 
Upvote 0
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
.....

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

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones