I have made an android app which working fine. I implemented the Login functionality in it. Can anyone guide me how to store the login credentials of the user so that the user does not have to login everytime he/she starts the app.
4 Answers
Here's an example from the Android developer site:
public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
setSilent(silent);
}
@Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", mSilentMode);
// Commit the edits!
editor.commit();
}
}
I recommend the Shared Preferences, but also take a look at the other options on that page to see if they fit your needs better.
- 931
- 12
- 21
I stored the credentials in sqlite database and checked it every time the app is started. My App has a logout button which cleared the credentials.
- First time when the db table is blank, present the login form
- Store the successful login in the db table
- Exit app
- Start app again
- Check if the login details exists
- If available, start straightaway or ask for login again
The design will vary based on your requirement of keeping a log of all logins etc
- 877
- 2
- 12
- 25
The most secure way is perhaps to implement some "cookie" mechanism so you don't have to really store the password locally. (Since there's no simple secure storage available to apps and almost everything is exposed to root.)
See Android Keychain for user credentials for some more pointers.
You might want to use some existing systems like openid or oauth so you don't have to worry about security yourself. See this blog if you can use Google account as login.