手機安全衛士------手機防盜頁面之自訂對話方塊&MD5加密

來源:互聯網
上載者:User

標籤:安卓自學   md5加密   自訂對話方塊   手機安全衛士   布局   

功能需求:

  • 使用者點擊首頁面上的“手機防盜”按鈕時,判斷使用者是否設定過密碼。
  • 如果沒有設定過,則彈出輸入密碼對話方塊
  • 如果設定過了,則彈出設定密碼對話方塊
  • 使用者的密碼要進行MD5加密之後再儲存在記憶體中

技術點:
- 自訂對話方塊的使用
- MD5加密的實現方式
- SharedPreferences的讀寫操作

自訂對話方塊
1.在layout目錄下建立一個布局檔案,把自訂的對話方塊布局設定成功
具體代碼實現如下

設定密碼對話方塊的布局代碼:

<?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="vertical"    >    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/tv_dialogsetuppwd_title"        android:text="設定密碼"        android:textSize="36sp"        android:textColor="#000"        android:background="#66ff6600"        android:gravity="center_horizontal"        />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="請輸入密碼"        android:inputType="textPassword"        android:id="@+id/et_dialogsetuppwd_pwd"        android:textSize="30sp"        android:textColor="#000"        />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="請輸入確認密碼"        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_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:id="@+id/btn_dialogsetuppwd_ok"            android:text="確定"            android:layout_marginRight="10dp"            android:layout_marginLeft="20dp"            android:textSize="28sp"            />        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:id="@+id/btn_dialogsetuppwd_cancel"            android:textSize="28sp"            android:text="取消"            android:layout_marginRight="20dp"            android:layout_marginLeft="10dp"            />    </LinearLayout></LinearLayout>

還有輸入密碼對話方塊的布局檔案,不再贅述。。

2.載入自訂布局到對話方塊的操作:

AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);        View view = View.inflate(HomeActivity.this,R.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,"密碼不可為空,請重新輸入",Toast.LENGTH_SHORT).show();                }                else                {                    String checkPassword = sp.getString("password",null);                    if(password.equals(checkPassword))                    {                        //進入首頁面                        dialog.dismiss();                    }                    else                    {                        Toast.makeText(HomeActivity.this,"密碼錯誤,請重新輸入",Toast.LENGTH_SHORT).show();                    }                }            }        });        mBtnCancel.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                dialog.dismiss();            }        });        dialog = builder.show();    }

邏輯:

  • 判斷password的值是否為空白,如果為空白,則 彈出設定密碼對話方塊;否則,彈出輸入密碼對話方塊。
  • 如果彈出的是設定密碼對話方塊,當兩次輸入的密碼相同且不為空白的時候,儲存密碼到SharedPreferences.
  • 如果彈出的是輸入密碼對話方塊,當使用者輸入的密碼和SharedPreferences中儲存的密碼相同時,跳轉到下一個頁面,否則,多士報錯

MD5加密
MD5加密是無法復原的,即加密後的密文無法轉換回原來的內容

MD5加密實現的思路:

  • 獲得一個資訊摘要器MessageDigest
  • 把資訊摘要器執行個體化,並確定採用MD5的加密方式
  • 把字串轉換成位元組數組
  • 把位元組數組通過digest()方法轉換成MD5加密後的位元組數組
  • 把數組遍曆
  • 對位元組數組中的每一個位元組進行與運算(加鹽),返回int類型
  • 調用Integer.toHexString(int) 把int類型轉換成十六進位字串
  • 如果字串的長度為1,則進行補零操作
  • 添加字串到StringBuffer中
  • StringBuffer調用toString()轉換成字串,即為結果。

程式碼範例:

//把資料進行MD5加密    public String getTextByMD5(String text)    {        StringBuffer buffer = new StringBuffer();        try        {            //1.建立一個資訊摘要器            MessageDigest digest = MessageDigest.getInstance("md5");            //2.調用digest方法,把資料進行MD5第一次加密            byte[] bytes = digest.digest(text.getBytes());            //3.遍曆數組            for(byte b : bytes)            {                //4.進行與運算                int number = b & 0xff;                //5.把運算結果轉換成十六進位字串                String hex = Integer.toHexString(number);                //6.添加字串到buffer                if(hex.length() == 1)                {                    buffer.append("0");                }                buffer.append(hex);            }        }        catch (NoSuchAlgorithmException e)        {            e.printStackTrace();        }        return buffer.toString();    }

不知不覺天亮了~~睡意襲來,五個小時之後繼續戰鬥!!

著作權聲明:剛出鍋的原創內容,希望對你有協助~

手機安全衛士------手機防盜頁面之自訂對話方塊&MD5加密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.