I am writing an application with registration data (username and password).
I want to make sure that when the user closes the application and starts it again, he should not enter his username and password again.
So here is my code. It does not seem to remember the password and username, unfortunately for me:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mPrefs = getSharedPreferences(PREFS, 0);
final CheckBox rememberMeCbx = (CheckBox)findViewById(R.id.saveLoginCheckBox);
boolean rememberMe = mPrefs.getBoolean("rememberMe", false);
if (android.os.Build.VERSION.SDK_INT > 9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
mXmlRpcClient = new XMLRPCClient(mXmlRpcUri);
mUsernameView = (EditText) findViewById(R.id.username);
mUsernameView.setText(mUsername);
mPasswordView = (EditText) findViewById(R.id.password);
mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int id,
KeyEvent keyEvent) {
if (id == R.id.login || id == EditorInfo.IME_NULL) {
attemptLogin();
return true;
}
return false;
}
});
mLoginFormView = findViewById(R.id.login_form);
mLoginStatusView = findViewById(R.id.login_status);
mLoginStatusMessageView = (TextView) findViewById(R.id.login_status_message);
findViewById(R.id.sign_in_button).setOnClickListener
(
new View.OnClickListener()
{
@Override
public void onClick(View view)
{
if(rememberMeCbx.isChecked())
{
attemptLogin();
saveLoginDetails();
}
else
{
attemptLogin();
removeLoginDetails();
}
}
});
if(rememberMe == true)
{
String login = mPrefs.getString("mUsername", null);
String upass = mPrefs.getString("mPassword", null);
if(login != null && upass != null)
{
EditText loginEbx = (EditText)findViewById(R.id.username);
EditText passEbx = (EditText)findViewById(R.id.password);
loginEbx.setText(login);
passEbx.setText(upass);
rememberMeCbx.setChecked(true);
}
}
}
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
try {
mSessionID = (String)mXmlRpcClient.call(mLoginFuncName, mUsername, mPassword);
return true;
} catch (Exception e) {
return false;
}
}
@Override
protected void onPostExecute(final Boolean success) {
mAuthTask = null;
showProgress(false);
if (success) {
finish();
Intent intent = new Intent(LoginActivity.this, MainWindow.class);
intent.putExtra("SessionID", mSessionID);
intent.putExtra("XmlRpcUrl", mXmlRpcUrl);
intent.putExtra("LoginFuncName", mLoginFuncName);
intent.putExtra("LogoutFuncName", mLogoutFuncName);
intent.putExtra("GetDevicesFuncName", mGetDevicesFuncName);
intent.putExtra("SendPositionFuncName", mSendPositionFuncName);
intent.putExtra("GetSavedTripFunc", mGetSavedTripFunc);
startActivity(intent);
} else {
mPasswordView
.setError(getString(R.string.error_incorrect_password));
mPasswordView.requestFocus();
}
}
}
private void saveLoginDetails()
{
EditText loginEbx = (EditText)findViewById(R.id.username);
EditText passEbx = (EditText)findViewById(R.id.password);
String login = loginEbx.getText().toString();
String upass = passEbx.getText().toString();
Editor e = mPrefs.edit();
e.putBoolean("rememberMe", true);
e.putString("login", login);
e.putString("password", upass);
e.commit();
}
private void removeLoginDetails()
{
Editor e = mPrefs.edit();
e.putBoolean("rememberMe", false);
e.remove("login");
e.remove("password");
e.commit();
}
}
Can you tell me what is wrong with my code, and how to improve it so that the username and password are saved and retrieved after closing and opening the application?
source
share