how to save login details of android app? - android

I have a android application which needs username and password to login. I need to save the username and password locally in phone or somewhere to use them when the user opens the app next time and logins to the app automatically without showing the login screen
EditText input1 = (EditText) findViewById(R.id.usertext);
EditText input2 = (EditText) findViewById(R.id.Passtext);
String username = input1.getText().toString();
String password = input2.getText().toString();
If the login is successful, it will call the activity through intent.

If you are using ApiLevel >= 5 read about AccountManager.

I would recomend to use something like MD5 or SHA1 for hashing your password before storing.
A possible place to store could either be "preferences" or the sqlite DB (not such usefull for only one single dataset)

To save username & password in sharepref try
SharedPreferences.Editor editor=mPreferences.edit();
private SharedPreferences mPreferences;
mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);
editor.putString("UserName", username);
editor.putString("PassWord", password);
editor.commit();

You can use preferences or a file if you don't really care about security. You should encrypt the password if you're gonna store them though.
See here for a better description of the options.

Related

How to have android app remember user data/profile?

Making a app that asks user to create a profile, wondering where I should get started in having the app remember this data user inputs? Any tutorials or suggestions would be appreciated.
Thanks,
Grant
I suggest you use SharedPreferences, unless you have a lot of information to store.
After the user successfully logged in, store his information in your Preferences.
For example, to store the username :
private SharedPreferences mPreferences;
mPreferences = getSharedPreferences("User", MODE_PRIVATE);
SharedPreferences.Editor editor = mPreferences.edit();
editor.putString("username", your_user_name);
editor.commit();
Each time the user accesses the login activity, you can check if the username is already stored in the preferences :
if (mPreferences.contains("username")) {
// start Main activity
} else {
// ask him to enter his credentials
}
When the user logs out, don't forget to delete the username key from your preferences :
SharedPreferences.Editor editor = mPreferences.edit();
editor.clear(); // This will delete all your preferences, check how to delete just one
editor.commit();
Insert the data to a SQLite database or use a plain file. The former is recommended for big apps.
Checked out SharedPreferences. Some examples here: SharedPreferences Tutorial
If you only have one user at once and just need to store simple user data like user name, email, id, you can store string/int/... format data in it. Or if you have server for storing user data, you can store credentials in SharedPreference and use the credentials to get data from your server.

Android Login with server integeration

In my application I am using a login process. In that I have username and password field. Once I login, the username and password will be there even when you exit the app or kill the app.
How can I set like that?
In android There is functionality of sharedpreferences You can save your username and password in shared preferences and check that if sharedpreferences are not null then display username and password in textField
You can use SharedPreference as below:
SharedPreferences m_sharedPreference=getSharedPreferences("MyPrefs",MODE_PRIVATE);
SharedPreferences.Editor m_editor =m_sharedPreference.edit();
m_editor.putString("UserName", m_etUsername.getText().toString());
m_editor.putString("Password", m_loetPassword.getText().toString());
m_editor.commit();

save username and password on sign up and log him/her in if entered correct id and password on next logins

as i m new to android application development ..
i m making a passwords manager type of application ...
so on the first page i have asked for username and password and a button for signup.
if a person is running application for first time then he/she has to sign up and provide user id,password,security question for the case if he/she forgets his/her password.
now i want to save the user id and password which user provide during signup in the application and next time when he/she again login and enters the user id and password then application should verify it from the saved user id and password and direct it to next page if he/she entered correct user id and password else a message should toast that "incorrect username or password ".
for this what coding in eclipse should i do..?
i had already made xml files i.e layouts..
thanks
You should save it in a file, i recommend using SharedPreferences and then read it from there each time they start the application, and see if the matches are correct or not.
To save it:
SharedPreferences sp = context.getSharedPreferences("loginSaved", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("username", "some user value");
editor.putString("password", "some password value");
editor.commit();
To get it:
SharedPreferences sp = context.getSharedPreferences("loginSaved", Context.MODE_PRIVATE);
String username = sp.getString("username", null);
String password = sp.getString("password", null);
if(username != null && password != null){
// login automatically with username and password
}
else{
// login for the first time
}

In android how to search that sharedpreference contains some value or not?

In android how to search that sharedpreference contains some value or not?
Actually I m making application which takes password and confirm password as fields.when user start app for first time he must enter both password and confirm password. But i want when user restart that app he must ask to enter only password.
For that i store password in sharedPreferences but now how do i know that their is already password exists in sharedPreferences or not?
so that if their is no password in sharedPreferences i can show the activity which contains both password and confirm password to enter AND if there exists password then i wl show activity that contains only password to enter.
If Anyone have idea then please help me.I m tring from many days but still not getting the output.
You can check it by using the contains method on your SharedPreferences instance:
boolean hasPassword = preference.contains("passwordKey");
API Docs:
public abstract boolean contains (String key)
Since: API Level 1
Checks whether the preferences contains a preference.
Parameters
key The name of the preference to check.
Returns
Returns true if the preference exists in the preferences, otherwise false.
for saving data...
SharedPreferences settings = getSharedPreferences("YourKey", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("password", passwordValue);
// Don't forget to commit your edits!!!
editor.commit();
for retrieving...
SharedPreferences settings =this.getSharedPreferences("YourKey", 0);
String userData = settings.getString("password", "0");
if((userData.equals("0"))){
//password has not been saved...
}
else{
//password is already there...
}
fist check if passwort is set:
boolean password_exists = !settings.getString("password","").equals("");
then hide or show the field for the second password
second_passwort_edittext.setVisibility(password_exists ? View.GONE:View.VISIBLE);
After entering the password you can change the behaviour with password_exists (compare the passwords and set them if false, compare with given stored password if true)
I think that you could just validate if you have received something by getting the password value from your shared preferences
if(preferences.getInt("storedPass", 0) != null) {
//Do Stuff
}

storing password using shared preference in android

I am using shared preference to store password but when ever my application starts it again ask preference value .i want once i have entered the preference password it should be fixed it should ask me again and again.
Where you want save your password use below code -
Editor editor = getSharedPreferences("password", 0).edit();
editor.putString("password", "your password");
editor.commit();
And where you like to get it back put below code there -
SharedPreferences pref = getSharedPreferences("password", 1);
String password = pref.getString("password", "");
If still its not working, Put your code out here.

Categories

Resources