Remember me in android application -
i working in remember me function in android have stored username , password in storage class,the storage class class implement shared preferences.
what have done
first checking if checkbox checked or not if ischecked store value in storage class(shared preferences).and in login service calling username password.
here code:
inside oncreate
boolean ischecked = true; login_submit_btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub loginservice(); } }); chkrememberme.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if(ischecked){ storage.saveusername(login.this, musername); storage.savepass(login.this, mpassword); } } }); } outside:
protected void loginservice() { // todo auto-generated method stub musername = login_email_edit.gettext().tostring().trim(); mpassword = login_password_edit.gettext().tostring().trim(); if (chkrememberme.ischecked()) { musername=storage.getusername(login.this); mpassword=storage.getpass(login.this); } if (musername.length() == 0) { alertdialog(" enter email ", login.this); } else if (iscontainsempty(musername)) { login_submit_btn.setenabled(true); alertdialog("valid email address", login.this); } else if(mpassword.length() == 0){ alertdialog("password should not blank", login.this); } } can figure out mistake .@thanks
your code doesn't seem right, saving credentials not removing them anywhere. if change username , password , remember me checked, code doesn't save credentials. remove setoncheckedchangelistener, of no use.
this should process:
when enter login activity, should check if credentials stored, if should show them in
textviews(username , password fields) , remember me checkbox should checked default.now if there no credentials stored (e.g. first time login) don't show , when user pressed login button , login successfull
2.1. if remember me checked should save credentials.
2.2. else if unchecked clear credentials
edit:
remove setoncheckedchangelistener
in oncreate method after
email=storage.getusername(login.this); password=storage.getpass(login.this); put
login_email_edit.settext(email); login_password_edit.settext(password); now inside login service
if(chkrememberme.ischecked()) { storage.saveusername(login.this, musername); storage.savepass(login.this, mpassword); }else { storage.saveusername(login.this, ""); storage.savepass(login.this, ""); } delete app , install again
Comments
Post a Comment