Last written to enter the mobile phone but the anti-theft interface requires a password limit, the first time you enter the dialog box prompts the user to set the password, re-entry will require the user to enter the password, this time to specifically achieve the above functions.
First login, set password
First of all, our password is stored in the sharepreference in the "Password" field, in the background need to verify that the field has been set password, if not set up the dialog box to let users set, otherwise user input password into the mobile phone anti-theft interface;
- Verify that the password is set
@Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); SP = getsharedpreferences ("config", context.mode_private); To interpret whether the user has set the password if (Ispwdsetup ()) { log.i (tag, "Set a password, pop-up dialog box for entering the password"), or else { log.i (tag, "No password is set, Popup Settings Password dialog box "); Showfirstentrydialog (); } } /** * Check sharedpreference for password settings * * @return * /Private Boolean Ispwdsetup () { String Password = sp.getstring ("password", null); if (password = = null) { return false; } else { if ("". Equals (password)) { return false; } else { return true;}} }
- Showfirstentrydialog (), Popup User Settings Password dialog box
/** * The dialog box that pops up when you first enter the program * Use custom dialog box style */private void Showfirstentrydialog () {dialog = New dialog (This, r.style.mydialog);//Dialog.setcontentview (R.layout.first_entry_dialog);//Set the content to be displayed view view = view.in Flate (this, r.layout.first_entry_dialog, null); Et_pwd = (EditText) View.findviewbyid (R.ID.ET_FIRST_ENTRY_PWD); Et_pwd_confirm = (EditText) View.findviewbyid (r.id.et_first_entry_pwd_confirm); Button bt_confirm = (button) View.findviewbyid (r.id.bt_first_dialog_confirm); Button Bt_cancel = (button) View.findviewbyid (R.id.bt_first_dialog_cancel); Set the button corresponding to the Click event Bt_confirm.setonclicklistener (this); Bt_cancel.setonclicklistener (this); Dialog.setcontentview (view); Dialog.setcanceledontouchoutside (FALSE)///Set Dialog to disappear Dialog.setcancelable (false) if no other place can be clicked,//Set dialog not be able to point back when the key disappears Dialog.show (); }
- after user input, the user's input is processed in the background
@Override public void OnClick (view view) {switch (View.getid ()) {//click Cancel Case R.id.bt_first_dialog _cancel:dialog.dismiss (); Break Case r.id.bt_first_dialog_confirm:string pwd = Et_pwd.gettext (). toString (). Trim (); String pwd_confirm = Et_pwd_confirm.gettext (). toString (). Trim (); Enter the password to wrap the null value if ("". Equals (pwd) | | "". Equals (pwd_confirm)) {Toast.maketext (Getapplicationcontext (), "Input cannot be empty! ", Toast.length_long). Show (); Return }else{if (pwd.equals (pwd_confirm)) {Editor editor = Sp.edit (); Editor.putstring ("password", pwd); Editor.commit (); }//Two input inconsistent else{Toast.maketext (Getapplicationcontext (), "two times input password is not the same!" ", Toast.length_long). Show (); Return }} DialOg.dismiss (); Break } }
The effect is as follows:
First entry into the mobile phone anti-theft interface:
Click OK when not entered:
Two input passwords are not the same:
Login again, enter your password
- Popup dialog box style: Normal_entry_dialog.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" 300dip "android:layout_height=" 280dip "android:gravity=" Center_horizontal "Android oid:orientation= "vertical" > <textview android:layout_width= "wrap_content" android:layout_height= "W Rap_content "android:text=" Login "android:textcolor=" @color/textcolor "android:textsize=" 24sp "/> <linearlayout android:layout_width= "300dip" android:layout_height= "Wrap_content" Android:backgroun D= "#ffc8c8c8" android:orientation= "vertical" > <edittext android:id= "@+id/et_normal_entry_p WD "Android:layout_width=" 300dip "android:layout_height=" wrap_content "android:hint=" Please enter the secret Code "android:password=" true "/> </LinearLayout> <linearlayout android:layout_width=" 300d IP "android:layout_height=" 50dip "android:gravity=" center "android:orientation=" Horizontal "> <button android:id = "@+id/bt_normal_dialog_confirm" android:layout_width= "140dip" android:layout_height= "40dip" android:background= "@drawable/button_background" android:text= "OK" android:textcolor= "#ffffffff" /> <button android:id= "@+id/bt_normal_dialog_cancel" android:layout_width= "140dip" android:layout_height= "40dip" android:layout_marginleft= "3dip" android:background= "@drawable /button_background "android:text=" Cancel "/> </LinearLayout></LinearLayout>
- Shownormalentrydialog Method
/** * The normal login dialog box * */ private void Shownormalentrydialog () { Dialog = New dialog (this, R.style.mydialog); View view = View.inflate (this, r.layout.normal_entry_dialog, null); Et_pwd = (EditText) View.findviewbyid (r.id.et_normal_entry_pwd); Button bt_confirm = (button) View.findviewbyid (r.id.bt_normal_dialog_confirm); Button Bt_cancel = (button) View.findviewbyid (r.id.bt_normal_dialog_cancel); Set the button corresponding to the Click event Bt_confirm.setonclicklistener (this); Bt_cancel.setonclicklistener (this); Dialog.setcontentview (view); Dialog.setcanceledontouchoutside (false);//Set dialog can not be clicked elsewhere when disappeared// dialog.setcancelable (FALSE);// Set dialog can not point back when the key disappears dialog.show (); }
@Override public void OnClick (view view) { switch (View.getid ()) { R.id.bt_normal_dialog_cancel: Dialog.dismiss (); break; Case r.id.bt_normal_dialog_confirm: String input_pwd = Et_pwd.gettext (). toString (); if ("". Equals (Input_pwd)) { Toast.maketext (Getapplicationcontext (), "Input cannot be empty! ", Toast.length_long). Show (); return; } else{ String password = sp.getstring ("Password", "" "); if (!password.equals (input_pwd)) { Toast.maketext (Getapplicationcontext (), "Input password is not correct, please re-enter!" ", Toast.length_long). Show (); Et_pwd.selectall ();//user input error, the text is selected all, convenient for users to delete re-enter return;} } LOG.I (TAG, "Loading mobile phone anti-theft main interface"); Dialog.dismiss (); break; } }
The effect is as follows:
Password Encrypted storage
Currently our password store is stored in clear text in sharepreference, so people with a bit of Android development can get the password we set.
Consider using an encryption algorithm to store passwords after they are encrypted.
Use the MessageDigest class provided by Javase to encrypt. The cryptographic algorithms supported by MessageDigest include: MD5, SHA-1, SHA-256.
pack Age Com.liuhao.mobilesafe.util;import Java.security.messagedigest;import java.security.NoSuchAlgorithmException; public class Md5encoder {public static string encode (string pwd) {try {messagedigest MD = messaged Igest.getinstance ("MD5"); byte[] bytes = Md.digest (Pwd.getbytes ()); StringBuffer sb = new StringBuffer (); for (byte b:bytes) {String str = integer.tohexstring (0xFF & B);//Byte is eight-byte stored, converted to 16 binary, directly with 11111111-phase With if (str.length () = = 1) {sb.append ("0" + str); } else {sb.append (str); }} return sb.tostring (); } catch (NoSuchAlgorithmException e) {e.printstacktrace (); throw new RuntimeException ("No Encryption algorithm exists"); }}}
This allows the password to be stored by invoking the Encode () method when the password is stored. The encrypted ciphertext is compared with the stored data when reading.
Editor.putstring ("Password", Md5encoder.encode (PWD)), if (!password.equals (Md5encoder.encode)) { Toast.maketext (Getapplicationcontext (), "Input password is not correct, please re-enter!" ", Toast.length_long). Show (); Et_pwd.selectall ();//user input error, the text is selected all to facilitate the user to delete re-enter return;}
In fact, we simply once encryption is also very non-insurance, although from the implementation of the algorithm to say MD5 encryption is irreversible, but some "ulterior motives" of the people, unexpectedly will all the predictable string corresponding to the ciphertext are calculated, really ...
Like this website: http://www.cmd5.com/
Stunned, there was wood there!
So, after the important site set the password must be set up a little more complicated!!!
"While doing project learning Android" Mobile security defender 07-mobile phone intrusion restrictions