Functional Requirements:
- When users click on the "Phone theft" button on the main page, they determine if the user has set a password.
- If not set, the Enter Password dialog box pops up
- If it is set, the Set Password dialog box pops up
- The user's password is MD5 encrypted and then stored in memory
Technical points:
-Use of custom dialog boxes
-How MD5 encryption is implemented
-Sharedpreferences Read and write operations
Customize dialog box
1. Create a layout file in the layouts directory to set the custom dialog box layout to successful
The specific code is implemented as follows
To set the layout code for the Password dialog box:
<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" android:layout_width =" match_parent " android:layout_height =" match_parent " android:orientation =; <textview android:layout _width = "match_parent" android:layout_height = "wrap_content" android:id = "@+id/tv_dialogsetuppwd_title" android:text = "Set password" android:textsize = "36sp" android:textcolor = "#000" Span class= "Hljs-attribute" >android:background = "#66ff6600" android:gravity =/> <edittext android:layout _width = "match_parent" android:layout_height = "wrap_content" android:hint = "Please enter password" android:inputtype = "Textpassword" android:id = "@+id/et_dialogsetuppwd_pwd" Span class= "Hljs-attribute" >android:textsize = "30sp" android:textcolor =" #000 "/> <edittext android:layout _width = "match_parent" android:layout_height = "wrap_content" android:hint = "Please enter a confirmation password" android:inputtype =" Textpassword " android:id =" @+id /et_dialogsetuppwd_checkpwd " android:textsize = "30SP" android:textcolor = "#000" /> <linearlayout android: Layout_width = "match_parent" android:layout_ Height = "wrap_content" android:orientation
= "horizontal" ; <button android:layout_w Idth = "0DP" android:layout_height =
"wrap_content"
android:layout_weight = "1" android:id = "@+id/btn_di Alogsetuppwd_ok " android:text =" OK " android:layout_marginright = "10DP" android:layout_marginleft = "20DP" android:textsize = "28SP" /> <button android:layout_w Idth = "0DP" android:layout_height =
"wrap_content"
android:layout_weight = "1" android:id = "@+id/btn_di Alogsetuppwd_cancel " android:textsize =" 28sp " android:text =" Cancel " android:layout_marginright = "20DP" android:layout_marginleft =" 10DP "/> </linearlayout></linearlayout>
There is also a layout file for the Password dialog box, which is no longer mentioned.
2. Load custom layouts into the dialog box:
Alertdialog. BuilderBuilder = new Alertdialog. Builder(homeactivity. this);View view = view. Inflate(homeactivity. thisR. Layout. Dialog_input_password,null);Builder. Setview(view);Metpwd = (EditText) view. Findviewbyid(R. ID. et_DIALOGINPUTPWD_PWD);Mbtnok = (Button) view. Findviewbyid(R. ID. BTN_DIALOGINPUTPWD_OK);Mbtncancel = (Button) view. Findviewbyid(R. ID. BTN_dialoginputpwd_cancel);Mbtnok. Setonclicklistener(New View. Onclicklistener() {public void OnClick (View v) {String password = metpwd. GetText(). toString(). Trim();if (textutils. IsEmpty(password)) {Toast. Maketext(homeactivity. this,"Password cannot be empty, please re-enter", Toast. LENGTH_short). Show();} else {String Checkpassword = SP. getString("Password", null);if (password. Equals(Checkpassword)) {//Enter the main Page dialog. Dismiss();} else {Toast. Maketext(homeactivity. this,"Password error, please re-enter", Toast. LENGTH_short). Show();} } } });Mbtncancel. Setonclicklistener(New View. Onclicklistener() {public void OnClick (View v) {dialog. Dismiss();} });Dialog = Builder. Show();}
Logic:
- Determines whether the value of the password is empty, or if it is empty, the Set Password dialog box pops up, otherwise, the Enter Password dialog box appears.
- If the Set Password dialog box pops up, the password is stored to sharedpreferences when the password is the same and not empty two times.
- If the Enter Password dialog box, when the user entered the password and sharedpreferences stored in the password in the same time, jump to the next page, otherwise, toast error
MD5 Encryption
MD5 encryption is irreversible, that is, encrypted ciphertext cannot be converted back to the original content
MD5 encryption implementation of the idea:
- Get a message digest MessageDigest
- Instantiate the information digest and determine the encryption method using MD5
- Convert a string to a byte array
- Converts a byte array through the digest () method to a MD5 encrypted byte array
- Iterating through the array
- Each byte in a byte array is combined with an operation (salt), which returns the int type
- Call integer.tohexstring (int) to convert the int type to a hexadecimal string
- If the length of the string is 1, make a 0 action
- Add string to StringBuffer
- The StringBuffer call to ToString () is converted to a string, which is the result.
code example:
//Data is MD5 encrypted PublicStringgetTextByMD5(String text) {StringBuffer buffer =NewStringBuffer ();Try{//1. Creating a message digestMessageDigest Digest = messagedigest.getinstance ("MD5");//2. Call the digest method to MD5 the data for first time encryption byte[] bytes = Digest.digest (Text.getbytes ());//3. Iterating through an array for(byteB:bytes) {//4. Conducting and computing intNumber = B &0xFF;//5. Converting the result of an operation into a hexadecimal stringString hex = integer.tohexstring (number);//6. Adding a string to buffer if(hex.length () = =1) {Buffer.append ("0"); } buffer.append (hex); } }Catch(NoSuchAlgorithmException e) {E.printstacktrace (); }returnBuffer.tostring (); }
Unconsciously morning ~ ~ Drowsiness attack, five hours after the fight!!
Copyright notice: Just out of the original content of the pot, I hope you have help ~
Mobile Security defender------Mobile anti-Theft page Custom dialog box &MD5 encryption