閱讀606 返回首頁    go 阿裏雲 go 技術社區[雲棲]


手機衛士05-自定義對話框


好,今天天我們在完成我們這個項目裏麵的一個自定義對話框的功能啦,它是在我們的第一個功能,手機防盜裏麵的,我們在給手機防盜那裏加一個登陸的操作,這樣會更安全一些,所以我們就用到了一個對話框,為了讓它更好看一些,而且也學習一下怎樣自定義對話框,所以我們就開始學習一下啦


首先,我們先給我們的手機防盜的啟動界麵,加一個快捷啟動的方式,就是在撥打電話的時候,輸入一個特定的號碼,然後就會啟動手機防盜那個界麵,聽起來,是不是很酷呢,其實很簡單的,就是用一個廣播接收都,來接收打電話時發出來的廣播,然後捕獲它,然後就可以進行我們想要的操作啦,好,直接上代碼

先新建一個類com.xiaobin.security.receiver.CallPhoneReceiver

  1. <font color="#333333"><font face="Arial">package com.xiaobin.security.receiver;

  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;

  5. import com.xiaobin.security.ui.LostProtectedActivity;

  6. public class CallPhoneReceiver extends BroadcastReceiver
  7. {

  8.         @Override
  9.         public void onReceive(Context context, Intent intent)
  10.         {
  11.                 String outPhoneNumber = this.getResultData();
  12.                 if(outPhoneNumber.equals("1314"))        //當監聽到用戶撥打的是1314的時候,就進行下麵的操作,你可以把撥打的號碼做成參數的形式,讓用戶可配置
  13.                 {
  14.                         Intent i = new Intent(context, LostProtectedActivity.class);
  15.                         //這個很重要,如果沒有這一句,那就會報錯,這一句是因為我們是在一個Receiver裏麵啟動一個activity的,但activity的啟動,都是放到一個棧裏麵的,
  16.                         //但Receiver裏麵沒有那個棧,所以我們要在這裏啟動一個activity,那就必須要指定這行代碼啦
  17.                         i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        
  18.                         context.startActivity(i);
  19.                         setResultData(null);        //這行代碼是把廣播的數據設置為null,這樣就不會把剛剛那個號碼撥打出去啦,隻會啟動我們的activity
  20.                 }
  21.         }

  22. }
  23. </font></font>
複製代碼
現在還不行的,我們還要在AndroidMainfest文件裏麵注冊這個廣播接收者
  1. <font color="#333333"><font face="Arial"><receiver
  2.             android:name="com.xiaobin.security.receiver.CallPhoneReceiver">
  3.             <intent-filter android:priority="1000"><!-- 把優先級設置高一些,以便第一個拿到廣播 -->
  4.                 <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
  5.             </intent-filter>
  6.         </receiver></font></font>
複製代碼
當然,我們還要加上相應的權限呢
  1. <font color="#333333"><font face="Arial"><uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/></font></font>
複製代碼
好啦,到現在為止,我們通過撥打電話,來啟動手機防盜這個功能就完成的啦,各位可以去試試 現在進入我們今天的重點,那就是自定義對話框啦,先上圖,看看我們的效果 其實,我們想自定義自己的對話框,那麼就有必要先去了解係統的對話框是怎樣寫的,在我們用到的android.jar這個包裏麵,就有係統自己寫的對話框的啦我們可以通過apktool這個工具來得到係統自己定義的資源文件apktool下載地址為了能運行apktool,我們要下載兩東西  apktool1.5.2.tar還有apktool-install-windows-r05-ibot.tar 下載完之後,直接它兩個解壓出來,然後把apktool1.5.2.tar裏麵的那個jar包,放到apktool-install-windows-r05-ibot.tar這個目錄下麵,如圖這樣子就會有3個文件的啦,現在我們把android.jar出拷貝到這個目錄下麵,然後在dos下麵進行這個目錄,然後就運行apktool d android.jar就這樣,我們就會在剛剛那個目錄下麵看到有一個解析出來的資源文件夾的啦,而我們係統自己寫的UI資源全在裏麵啦,我們有空的話,可以去看看,係統是怎樣自己寫的,然後自己就可以相應的進行修改啦好啦,說回我們今天的對話框,係統自己寫的對話框,其實就是一個主題,要用的時候,就使用那個主題,那麼,它就是在res/value/styles.xml裏麵的,我們把那個文件打開,然後搜索Theme.Dialog,然後就可以看到係統自己寫的對話框啦 好啦,知道這些之後,我們有空就可以參考一下怎樣寫啦現在我們自己來寫,我們在我們的項目裏麵的styles.xml文件裏麵寫上我們自己的對話框的樣式啦
  1.     <style name="MyDialog" parent="@android:style/Theme.Dialog"><!-- 要繼承係統原來的對話框 -->
  2.         
  3.         <item name="android:windowBackground">@drawable/title_background</item><!-- 指定背景顏色 -->
  4.         <item name="android:windowNoTitle">true</item><!-- 隱藏係統原來的標題欄 -->
  5.         
  6.     </style>
複製代碼
因為我是教大家怎樣自定義對話框,所以為了簡單,我隻是寫了這兩個屬性而已,有興趣的,你們可以參考一下係統自己的,然後寫多一點,寫好我們的樣式之後,怎樣用呢其實很簡單,隻要在new一個dialog的時候,指定一下它的樣式就行的啦
  1. dialog = new Dialog(this, R.style.MyDialog);
複製代碼
好啦,我們現在就結合我們今天的邏輯來看一下,我們的對話框的效果,我們今天要做的是,當用戶第一次啟動這個手機防盜這個功能的時候,要求用戶輸入一個登錄密碼,然後以後進入這個功能的時候,都要輸入以前設置的密碼,而且我們的密碼是以MD5加密後,存放到SharedPreferences裏麵的既然是加密,那麼我們現在就先來寫一個加密的工具類啦com.xiaobin.security.utils.MD5Encoder
  1. package com.xiaobin.security.utils;

  2. import java.security.MessageDigest;
  3. import java.security.NoSuchAlgorithmException;

  4. public class MD5Encoder
  5. {
  6.         public static String encode(String pwd)
  7.         {
  8.                 try
  9.                 {
  10.                         MessageDigest messageDigest = MessageDigest.getInstance("MD5");//拿到MD5加密的對象
  11.                         byte[] bytes = messageDigest.digest(pwd.getBytes());//返回一個加密後的字節數組
  12.                         StringBuffer sb = new StringBuffer();
  13.                         String tmp;
  14.                         for(int i = 0; i < bytes.length; i++)
  15.                         {
  16.                                 tmp = Integer.toHexString(0xff & bytes[i]);//把字節轉換為16進製的字符串
  17.                                 if(tmp.length() == 1)        //如果這個字符串,隻有一個字符,就要補0
  18.                                 {
  19.                                         sb.append("0" + tmp);
  20.                                 }
  21.                                 else
  22.                                 {
  23.                                         sb.append(tmp);
  24.                                 }
  25.                         }
  26.                         return sb.toString();
  27.                 }
  28.                 catch (NoSuchAlgorithmException e)
  29.                 {
  30.                         throw new RuntimeException("沒有這個加密算法" + e);
  31.                 }
  32.         }

  33. }

複製代碼
好啦,接下來,都是一些很簡單的邏輯處理而已,我們直接上代碼com.xiaobin.security.ui.LostProtectedActivity
  1. package com.xiaobin.security.ui;

  2. import android.app.Activity;
  3. import android.app.Dialog;
  4. import android.content.Context;
  5. import android.content.SharedPreferences;
  6. import android.content.SharedPreferences.Editor;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.Toast;

  13. import com.xiaobin.security.R;
  14. import com.xiaobin.security.utils.MD5Encoder;

  15. public class LostProtectedActivity extends Activity implements OnClickListener
  16. {
  17.         private SharedPreferences sp;
  18.         private Dialog dialog;
  19.         private EditText password;
  20.         private EditText confirmPassword;
  21.         
  22.         @Override
  23.         protected void onCreate(Bundle savedInstanceState)
  24.         {
  25.                 super.onCreate(savedInstanceState);
  26.                
  27.                 sp = getSharedPreferences("cofig", Context.MODE_PRIVATE);
  28.                
  29.                 if(isSetPassword())
  30.                 {
  31.                         showLoginDialog();
  32.                 }
  33.                 else
  34.                 {
  35.                         showFirstDialog();
  36.                 }
  37.         }
  38.         
  39.         private void showLoginDialog()
  40.         {
  41.                 dialog = new Dialog(this, R.style.MyDialog);
  42.                 View view = View.inflate(this, R.layout.login_dialog, null);
  43.                 password = (EditText) view.findViewById(R.id.et_protected_password);
  44.                 Button yes = (Button) view.findViewById(R.id.bt_protected_login_yes);
  45.                 Button cancel = (Button) view.findViewById(R.id.bt_protected_login_no);
  46.                 yes.setOnClickListener(this);
  47.                 cancel.setOnClickListener(this);
  48.                 dialog.setContentView(view);
  49.                 dialog.show();
  50.         }

  51.         private void showFirstDialog()
  52.         {
  53.                 dialog = new Dialog(this, R.style.MyDialog);
  54.                 //dialog.setContentView(R.layout.first_dialog);
  55.                 View view = View.inflate(this, R.layout.first_dialog, null);        //這樣來填充一個而已文件,比較方便
  56.                 password = (EditText) view.findViewById(R.id.et_protected_first_password);
  57.                 confirmPassword = (EditText) view.findViewById(R.id.et_protected_confirm_password);
  58.                 Button yes = (Button) view.findViewById(R.id.bt_protected_first_yes);
  59.                 Button cancel = (Button) view.findViewById(R.id.bt_protected_first_no);
  60.                 yes.setOnClickListener(this);
  61.                 cancel.setOnClickListener(this);
  62.                 dialog.setContentView(view);
  63.                 dialog.show();
  64.         }

  65.         private boolean isSetPassword()
  66.         {
  67.                 String pwd = sp.getString("password", "");
  68.                 if(pwd.equals("") || pwd == null)
  69.                 {
  70.                         return false;
  71.                 }
  72.                 return true;
  73.         }

  74.         @Override
  75.         public void onClick(View v)
  76.         {
  77.                 switch(v.getId())
  78.                 {
  79.                         case R.id.bt_protected_first_yes :
  80.                                 String fp = password.getText().toString().trim();
  81.                                 String cp = confirmPassword.getText().toString().trim();
  82.                                 if(fp.equals("") || cp.equals(""))
  83.                                 {
  84.                                         Toast.makeText(this, "密碼不能為空", Toast.LENGTH_SHORT).show();
  85.                                         return;
  86.                                 }
  87.                                 else
  88.                                 {
  89.                                         if(fp.equals(cp))
  90.                                         {
  91.                                                 Editor editor = sp.edit();
  92.                                                 editor.putString("password", MD5Encoder.encode(fp));
  93.                                                 editor.commit();
  94.                                         }
  95.                                         else
  96.                                         {
  97.                                                 Toast.makeText(this, "兩次密碼不相同", Toast.LENGTH_SHORT).show();
  98.                                                 return;
  99.                                         }
  100.                                 }
  101.                                 dialog.dismiss();
  102.                                 break;
  103.                                 
  104.                         case R.id.bt_protected_first_no :
  105.                                 dialog.dismiss();
  106.                                 finish();
  107.                                 break;
  108.                                 
  109.                         case R.id.bt_protected_login_yes :
  110.                                 String pwd = password.getText().toString().toString();
  111.                                 if(pwd.equals(""))
  112.                                 {
  113.                                         Toast.makeText(this, "請輸入密碼", Toast.LENGTH_SHORT).show();
  114.                                 }
  115.                                 else
  116.                                 {
  117.                                         String str = sp.getString("password", "");
  118.                                         if(MD5Encoder.encode(pwd).equals(str))
  119.                                         {
  120.                                                 dialog.dismiss();
  121.                                         }
  122.                                         else
  123.                                         {
  124.                                                 Toast.makeText(this, "密碼錯誤", Toast.LENGTH_SHORT).show();
  125.                                         }
  126.                                 }
  127.                                 break;
  128.                                 
  129.                         case R.id.bt_protected_login_no :
  130.                                 dialog.dismiss();
  131.                                 finish();
  132.                                 break;
  133.                                 
  134.                         default :
  135.                                 break;
  136.                 }
  137.         }

  138. }

複製代碼
另外,還有兩個布局文件,一個是第一次登錄時,設置密碼的,一個是以後登錄時,輸入密碼的first_dialog
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:andro
  3.     android:layout_width="300dip"
  4.     android:layout_height="280dip"
  5.     android:gravity="center_horizontal"
  6.     android:orientation="vertical" >
  7.    
  8.     <TextView
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"
  11.         android:textColor="@android:color/white"
  12.         android:textSize="24sp"
  13.         android:text="@string/setPassword"/>
  14.    
  15.     <LinearLayout
  16.         android:layout_width="300dip"
  17.         android:layout_height="180dip"
  18.         android:background="#ffc8c8c8"
  19.         android:orientation="vertical">
  20.         
  21.         <TextView
  22.             android:layout_width="wrap_content"
  23.             android:layout_height="wrap_content"
  24.             android:text="@string/setPasswordDescription"/>
  25.         
  26.         <EditText
  27.             android:
  28.             android:layout_width="match_parent"
  29.             android:layout_height="wrap_content"
  30.             android:inputType="textPassword"/>
  31.         
  32.         <TextView
  33.             android:layout_width="wrap_content"
  34.             android:layout_height="wrap_content"
  35.             android:text="@string/againPassword"/>
  36.         
  37.         <EditText
  38.             android:
  39.             android:layout_width="match_parent"
  40.             android:layout_height="wrap_content"
  41.             android:inputType="textPassword"/>
  42.         
  43.         <LinearLayout
  44.             android:layout_width="300dip"
  45.             android:layout_height="50dip"
  46.             android:gravity="center"
  47.             android:orientation="horizontal">
  48.             
  49.             <Button
  50.                 android:
  51.                 android:layout_width="140dip"
  52.                 android:layout_height="40dip"
  53.                 android:text="@string/protectedYes"/>
  54.             
  55.             <Button
  56.                 android:
  57.                 android:layout_width="140dip"
  58.                 android:layout_height="40dip"
  59.                 android:layout_marginLeft="10dip"
  60.                 android:text="@string/protectedNo"/>
  61.             
  62.         </LinearLayout>
  63.         
  64.     </LinearLayout>

  65. </LinearLayout>

複製代碼
login_dialog.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:andro
  3.     android:layout_width="300dip"
  4.     android:layout_height="180dip"
  5.     android:gravity="center_horizontal"
  6.     android:orientation="vertical" >
  7.    
  8.     <TextView
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"
  11.         android:textColor="@android:color/white"
  12.         android:textSize="24sp"
  13.         android:text="@string/login"/>
  14.    
  15.     <LinearLayout
  16.         android:layout_width="300dip"
  17.         android:layout_height="120dip"
  18.         android:background="#ffc8c8c8"
  19.         android:orientation="vertical">
  20.         
  21.         <TextView
  22.             android:layout_width="wrap_content"
  23.             android:layout_height="wrap_content"
  24.             android:text="@string/inputPassword"/>
  25.         
  26.         <EditText
  27.             android:
  28.             android:layout_width="match_parent"
  29.             android:layout_height="wrap_content"
  30.             android:inputType="textPassword"/>
  31.         
  32.         <LinearLayout
  33.             android:layout_width="300dip"
  34.             android:layout_height="50dip"
  35.             android:gravity="center"
  36.             android:orientation="horizontal">
  37.             
  38.             <Button
  39.                 android:
  40.                 android:layout_width="140dip"
  41.                 android:layout_height="40dip"
  42.                 android:text="@string/protectedYes"/>
  43.             
  44.             <Button
  45.                 android:
  46.                 android:layout_width="140dip"
  47.                 android:layout_height="40dip"
  48.                 android:layout_marginLeft="10dip"
  49.                 android:text="@string/protectedNo"/>
  50.             
  51.         </LinearLayout>
  52.         
  53.     </LinearLayout>

  54. </LinearLayout>

複製代碼
好啦,今天的代碼就到這裏啦,代碼有點多,有什麼不明白的,歡迎留言,有什麼要指導的,也歡迎留言!!!
Security_05自定義對話框與撥打電話來啟動activity.rar(421.51 KB, 下載次數: 173)

最後更新:2017-04-03 14:54:43

  上一篇:go “爆發戶”小米憑啥那麼牛
  下一篇:go [LeetCode]136.Single Numbe