[Learn Android while working on projects] mobile phone security guard 07-mobile phone anti-theft access restriction, android07-

Source: Internet
Author: User

[Learn Android while working on projects] mobile phone security guard 07-mobile phone anti-theft access restriction, android07-

Password restrictions are required when you enter the mobile phone but the anti-theft interface last time. The first time you enter the mobile phone, a dialog box is displayed prompting you to set the password. When you enter the mobile phone again, you are required to enter the password; this time we will implement the above functions.

Set the password for the First Login

First, our password is saved in the "password" field in the SharePreference. During logon, the background needs to check whether the password has been set for this field, if this parameter is not set, a dialog box is displayed to allow you to set the parameter. Otherwise, you must enter a password to access the anti-theft page of the mobile phone;

  • Check whether a password is set
@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); sp = getSharedPreferences ("config", Context. MODE_PRIVATE); // determines whether the user has set the password if (isPwdSetup () {Log. I (TAG, "set password, pop-up password input dialog box");} else {Log. I (TAG, "no password is set, the Set Password dialog box is displayed"); showFirstEntryDialog ();}} /*** check whether the sharedpreference has a password setting ** @ 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 (). The user password Setting dialog box is displayed.
/*** Password Setting dialog Box * use custom Dialog box style */private void showFirstEntryDialog () {dialog = new Dialog (this, R. style. myDialog); // dialog. setContentView (R. layout. first_entry_dialog); // you can specify the value of View. inflate (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); // click the event bt_confirm.setOnClickListener (this); bt_cancel.setOnClickListener (this); dialog. setContentView (view); dialog. setCanceledOnTouchOutside (false); // set the dialog to disappear when you cannot click other places. setCancelable (false); // you cannot set the dialog to disappear when you click the return key. show ();}


  • After user input, the background processes user input
@ 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 (); // The null value if ("". equals (pwd) | "". equals (pwd_confirm) {Toast. makeText (getApplicationContext (), "Enter cannot be blank! ", Toast. LENGTH_LONG ). show (); return;} else {if (pwd. equals (pwd_confirm) {Editor editor = sp. edit (); editor. putString ("password", pwd); editor. commit ();} // The two inputs are inconsistent with else {Toast. makeText (getApplicationContext (), "The two passwords are different! ", Toast. LENGTH_LONG). show (); return ;}} dialog. dismiss (); break ;}}


The effect is as follows:

Enter the mobile phone anti-theft page for the first time:

Click "OK" when the data is not lost:

The two passwords are different:

Log on again and enter the password
  • 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: orientation = "vertical"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "login" android: textColor = "@ color/textcolor" android: textSize = "24sp"/> <LinearLayout android: layout_width = "300dip" android: layout_height = "wrap_content" android: background = "# ffc8c8c8" android: orientation = "vertical"> <EditText android: id = "@ + id/et_normal_entry_pwd" android: layout_width = "300dip" android: layout_height = "wrap_content" android: hint = "enter the password" android: password = "true"/> </LinearLayout> <LinearLayout android: layout_width = "300dip" 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>


  • ShowNormalEntryDialog Method
/*** Normal logon 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); // click the event bt_confirm.setOnClickListener (this); bt_cancel.setOnClickListener (this); dialog. setContentView (view); dialog. setCanceledOnTouchOutside (false); // set the dialog to disappear when you cannot click elsewhere. // dialog. setCancelable (false); // you cannot set the dialog to disappear when you click the return key. show ();}


  • Button processing:
@ Override public void onClick (View view) {switch (view. getId () {case 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 (), "Enter cannot be blank! ", Toast. LENGTH_LONG). show (); return;} else {String password = sp. getString (" password "," "); if (! Password. equals (input_pwd) {Toast. makeText (getApplicationContext (), "the password is incorrect. Please enter it again! ", Toast. LENGTH_LONG ). show (); et_pwd.selectAll (); // After the user inputs an error, select all the text for deletion and re-enter return ;}} Log. I (TAG, "loading mobile phone anti-theft Main Interface"); dialog. dismiss (); break ;}}


The effect is as follows:

Encrypted password storage

Currently, all of our passwords are stored in the SharePreference in plain text. Therefore, people with a little Android development foundation can obtain the password we set.

Consider using encryption algorithms to store encrypted passwords.

Use the MessageDigest class provided by JavaSe for encryption. MessageDigest supports the following encryption algorithms: MD5, SHA-1, SHA-256.

Package com. liuhao. mobilesafe. util; import java. security. messageDigest; import java. security. noSuchAlgorithmException; public class MD5Encoder {public static String encode (String pwd) {try {MessageDigest md = MessageDigest. getInstance ("MD5"); byte [] bytes = md. digest (pwd. getBytes (); StringBuffer sb = new StringBuffer (); for (byte B: bytes) {String str = Integer. toHexString (0xff & B); // byte is stored in eight bytes, which is converted to a hexadecimal number. It is directly connected to the 11111111 and 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 ");}}}


In this way, you can store the password by calling the encode () method when storing the password. The encrypted ciphertext should also be used for comparison with the stored ciphertext during reading.

Editor. putString ("password", MD5Encoder. encode (pwd); if (! Password. equals (MD5Encoder. encode (input_pwd) {Toast. makeText (getApplicationContext (), "the password is incorrect. Please enter it again! ", Toast. LENGTH_LONG). show (); et_pwd.selectAll (); // After the user inputs an error, select all the text to facilitate deletion and re-input return ;}


In fact, it is not safe for us to simply encrypt it once. Although md5 encryption is irreversible in algorithm implementation, some people who "have ulterior motives, I figured out all the secrets corresponding to the foreseeable strings...

Such as this Website: http://www.cmd5.com/

Stunned!

Therefore, it will be more complicated to set passwords for important websites in the future !!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.