How Do I Use Shared Pref File For My App's Log In Page
In my application I got user info in shared pref file. When user enter their details on the log in page, I want only users whose info is in shared pref file to log in. How can I do
Solution 1:
You can use your apps shared preferences. The prefs can be accessed anytime using the key you set for these prefs.
staticfinalStringKEY_USERNAME="username";
staticfinalStringKEY_PASSWORD="password";
if (saveLogInDetail) { //save username and pw to prefsSharedPreferencesprefs= PreferenceManager.getDefaultSharedPreferences(this);
Editored= prefs.edit();
ed.putString(KEY_USERNAME, theUsername);
ed.putString(KEY_PASSWORD, thePW);
ed.commit();
}
To access the information or check the valid username and password use this:
SharedPreferencesprefs= PreferenceManager.getDefaultSharedPreferences(this);
StringstoredUsername= prefs.getString(KEY_USERNAME, "Default Value if not found");
StringstoredPassword= prefs.getString(KEY_PASSWORD, ""); //return nothing if no pass saved
Solution 2:
Store your login data in sharedpreferences: String username="dipali"; String pass="1111";
SharedPreferences.Editoreditor= viewLoginScreen
.getSharedPreferences(
"prefernce",
Context.MODE_PRIVATE).edit();
editor.putString(
"username",
user);
editor.commit();
SharedPreferences.Editoreditor= viewLoginScreen
.getSharedPreferences(
"prefernce",
Context.MODE_PRIVATE).edit();
editor.putString(
"pass",
pass);
editor.commit();
get shared prefences data:
SharedPreferences shar = viewLoginScreen
.getSharedPreferences(
"prefernce",
Context.MODE_PRIVATE);
String username=shar.getString("username","");
String pass=shar.getString("pass","");
Solution 3:
you should use SQlite Database instead of using share preference. Once you set up database then insert user credentials to database if user is a not register. And in your case check if current user is in database if exits authenticate him other wise you can force him to register to application. You can see sqlite in detail here http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
Post a Comment for "How Do I Use Shared Pref File For My App's Log In Page"