Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

Get excited for the Samsung Galaxy S5! Find everything you need and discuss it in our Galaxy S5 Forum!

Like Tree3Likes
  • 1 Post By iowabowtech
  • 2 Post By shredcode

test: Reply
 
LinkBack Thread Tools
Old April 10th, 2013, 01:11 AM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Mar 2013
Posts: 10
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Question how to create a session for remember me (login authentication) in android

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();
}
});
}

Amalthomas is offline  
Reply With Quote
sponsored links
Old April 10th, 2013, 01:21 AM   #2 (permalink)
New Member
 
Join Date: Apr 2013
Location: Bangalore
Gender: Male
Posts: 1
 
Device(s):
Carrier: Android Application Developer

Thanks: 0
Thanked 0 Times in 0 Posts
Default

use sharedPreference Tutorial u'll get the answer
ayazshakeeb is offline  
Reply With Quote
Old April 11th, 2013, 11:59 PM   #3 (permalink)
New Member
Thread Author (OP)
 
Join Date: Mar 2013
Posts: 10
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

@ayazshakeeb, can u guide me to use sharedPreference. i am new to android.
Amalthomas is offline  
Reply With Quote
Old April 12th, 2013, 12:48 AM   #4 (permalink)
Carpe Cerevisi
 
iowabowtech's Avatar
 
Join Date: May 2010
Location: In The Shadows
Posts: 9,472
 
Device(s): LG G2
Carrier: VZW

Thanks: 4,727
Thanked 6,561 Times in 3,489 Posts
Default

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

Cheers,
IBT
Amalthomas likes this.
iowabowtech is offline  
Reply With Quote
Old April 12th, 2013, 05:51 PM   #5 (permalink)
Junior Member
 
Join Date: Mar 2013
Posts: 30
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 3 Times in 2 Posts
Default

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]
iowabowtech and Amalthomas like this.
shredcode is offline  
Reply With Quote
Old April 12th, 2013, 11:28 PM   #6 (permalink)
New Member
Thread Author (OP)
 
Join Date: Mar 2013
Posts: 10
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

@shredcode, Thank you
Amalthomas is offline  
Reply With Quote
Old April 15th, 2013, 05:07 AM   #7 (permalink)
New Member
Thread Author (OP)
 
Join Date: Mar 2013
Posts: 10
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
.....
Amalthomas is offline  
Reply With Quote
Old April 15th, 2013, 06:48 PM   #8 (permalink)
Junior Member
 
Join Date: Mar 2013
Posts: 30
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 3 Times in 2 Posts
Default

Quote:
Originally Posted by Amalthomas View Post
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?
shredcode is offline  
Reply With Quote
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 08:19 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.