java - Android App storing and calling Username and Passwords -
i have application want create log in activity. ive got 1) login (button) 2) userid (edittext) 3) password (edittext)
i store username , password on device's internal storage. how create file sharedpreferences pulls , encrypt it?
login_button.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view v) { sharedpreferences userdetails = getsharedpreferences("userdetails", mode_private); sharedpreferences.editor edit = userdetails.edit(); edit.clear(); if(etuserid.gettext().tostring().equals("")){ toast.maketext(getapplicationcontext(),"please enter user id", toast.length_long).show(); }else if (etpassword.gettext().tostring().equals("")){ toast.maketext(getapplicationcontext(),"please enter password", toast.length_long).show(); }else{ edit.putstring("userid", txtuid.gettext().tostring().trim()); edit.putstring("password", txtpass.gettext().tostring().trim()); edit.commit(); } } }); the txtuid , txtpass showing red , when delete system says gettext wrong.
this first shot @ authenticator. found called passwordauthentication android dev page haven't been able find tutorials or code of being used.
sharedpreferences userdetails = getsharedpreferences("userdetails", mode_private); string uid = userdetails.getstring("userid", ""); string pass = userdetails.getstring("password", ""); if (userdetails.contains("userid")||userdetails.contains("password")){ startactivity(new intent(logon.this,crimemap.class)); } sources ive used research: http://developer.android.com/reference/java/net/passwordauthentication.html how use getsharedpreferences in android
update:
ok, here updated code. if don't enter user id or password sets off statements. however, program doesn't go next activity when put in correct userid , password. i'm going running first , ill move on encrypting it.
@override protected void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.activity_log_on);
etuserid = (edittext) findviewbyid(r.id.etuserid); etpassword = (edittext) findviewbyid(r.id.etpassword); login_button = (button) findviewbyid(r.id.blogin); login_button.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view v) { int uid = 1155; string pass = "pass"; sharedpreferences userdetails = getsharedpreferences(user_file, mode_private); sharedpreferences.editor edit = userdetails.edit(); edit.putint("userid", uid); edit.putstring("password", pass); edit.commit(); if((etuserid.gettext().tostring().equals("")) || (etpassword.gettext().tostring().equals(""))) { toast.maketext(getapplicationcontext(),"please enter user id", toast.length_long).show(); }else if (etpassword.gettext().tostring().equals("")){ toast.maketext(getapplicationcontext(),"please enter password", toast.length_long).show(); }else{ edit.putint("userid", integer.parseint(etuserid.gettext().tostring())); edit.putstring("password", etpassword.gettext().tostring()); } } }); sharedpreferences userdetails = getsharedpreferences(user_file, mode_private); if (userdetails.equals(etuserid) && (userdetails.equals(etpassword))){ startactivity(new intent(logon.this,crimemap.class)); } update:
i got it! help! here complete code portion of project. still haven't encrypted think i'm going take break , come later. help!
@override public void onclick(view v) { int uid = 1155; string pass = "pass"; sharedpreferences userdetails = getsharedpreferences(user_file, mode_private); sharedpreferences.editor edit = userdetails.edit(); edit.putint("userid", uid); edit.putstring("password", pass); edit.commit(); if((etuserid.gettext().tostring().equals(""))){ toast.maketext(getapplicationcontext(),"please enter user id", toast.length_long).show(); }else if (etpassword.gettext().tostring().equals("")){ toast.maketext(getapplicationcontext(),"please enter password", toast.length_long).show(); }else{ string user_id = etuserid.gettext().tostring(); int user_id2 = integer.parseint(user_id); string user_password = etpassword.gettext().tostring(); int userid = userdetails.getint("userid", 1); string password = userdetails.getstring("password", "no name"); if (userid == user_id2 && password.equals(user_password)){ startactivity(new intent(logon.this,crimemap.class)); } }
don't try access file. let android that, , encrypt data store shared preferences. mean:
edit.putstring("userid", encrypt(txtuid.gettext().tostring().trim())); edit.putstring("password", encrypt(txtpass.gettext().tostring().trim())); you can use whatever encryption like, though of course you'll need decode way.
another option store sha1 encrypted password, , in authentication compare sha1 encypted password typed in stored string. use http://mvnrepository.com/artifact/commons-codec/commons-codec example:
string sha1password = digestutils.sha1hex(password); edit.putstring("sha1password", encrypt(sha1password); to fix "red" problem you'll need declare txtuid, txtpass. should added layout somehow. via editing layout xml. programmatically can do, like: input text dialog android
Comments
Post a Comment