手機衛士05-自定義對話框
好,今天天我們在完成我們這個項目裏麵的一個自定義對話框的功能啦,它是在我們的第一個功能,手機防盜裏麵的,我們在給手機防盜那裏加一個登陸的操作,這樣會更安全一些,所以我們就用到了一個對話框,為了讓它更好看一些,而且也學習一下怎樣自定義對話框,所以我們就開始學習一下啦
首先,我們先給我們的手機防盜的啟動界麵,加一個快捷啟動的方式,就是在撥打電話的時候,輸入一個特定的號碼,然後就會啟動手機防盜那個界麵,聽起來,是不是很酷呢,其實很簡單的,就是用一個廣播接收都,來接收打電話時發出來的廣播,然後捕獲它,然後就可以進行我們想要的操作啦,好,直接上代碼
先新建一個類com.xiaobin.security.receiver.CallPhoneReceiver
- <font color="#333333"><font face="Arial">package com.xiaobin.security.receiver;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import com.xiaobin.security.ui.LostProtectedActivity;
- public class CallPhoneReceiver extends BroadcastReceiver
- {
- @Override
- public void onReceive(Context context, Intent intent)
- {
- String outPhoneNumber = this.getResultData();
- if(outPhoneNumber.equals("1314")) //當監聽到用戶撥打的是1314的時候,就進行下麵的操作,你可以把撥打的號碼做成參數的形式,讓用戶可配置
- {
- Intent i = new Intent(context, LostProtectedActivity.class);
- //這個很重要,如果沒有這一句,那就會報錯,這一句是因為我們是在一個Receiver裏麵啟動一個activity的,但activity的啟動,都是放到一個棧裏麵的,
- //但Receiver裏麵沒有那個棧,所以我們要在這裏啟動一個activity,那就必須要指定這行代碼啦
- i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(i);
- setResultData(null); //這行代碼是把廣播的數據設置為null,這樣就不會把剛剛那個號碼撥打出去啦,隻會啟動我們的activity
- }
- }
- }
- </font></font>
- <font color="#333333"><font face="Arial"><receiver
- android:name="com.xiaobin.security.receiver.CallPhoneReceiver">
- <intent-filter android:priority="1000"><!-- 把優先級設置高一些,以便第一個拿到廣播 -->
- <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
- </intent-filter>
- </receiver></font></font>
- <font color="#333333"><font face="Arial"><uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/></font></font>






- <style name="MyDialog" parent="@android:style/Theme.Dialog"><!-- 要繼承係統原來的對話框 -->
-
- <item name="android:windowBackground">@drawable/title_background</item><!-- 指定背景顏色 -->
- <item name="android:windowNoTitle">true</item><!-- 隱藏係統原來的標題欄 -->
-
- </style>
- dialog = new Dialog(this, R.style.MyDialog);
- package com.xiaobin.security.utils;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- public class MD5Encoder
- {
- public static String encode(String pwd)
- {
- try
- {
- MessageDigest messageDigest = MessageDigest.getInstance("MD5");//拿到MD5加密的對象
- byte[] bytes = messageDigest.digest(pwd.getBytes());//返回一個加密後的字節數組
- StringBuffer sb = new StringBuffer();
- String tmp;
- for(int i = 0; i < bytes.length; i++)
- {
- tmp = Integer.toHexString(0xff & bytes[i]);//把字節轉換為16進製的字符串
- if(tmp.length() == 1) //如果這個字符串,隻有一個字符,就要補0
- {
- sb.append("0" + tmp);
- }
- else
- {
- sb.append(tmp);
- }
- }
- return sb.toString();
- }
- catch (NoSuchAlgorithmException e)
- {
- throw new RuntimeException("沒有這個加密算法" + e);
- }
- }
- }
- package com.xiaobin.security.ui;
- import android.app.Activity;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.content.SharedPreferences.Editor;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.xiaobin.security.R;
- import com.xiaobin.security.utils.MD5Encoder;
- public class LostProtectedActivity extends Activity implements OnClickListener
- {
- private SharedPreferences sp;
- private Dialog dialog;
- private EditText password;
- private EditText confirmPassword;
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
-
- sp = getSharedPreferences("cofig", Context.MODE_PRIVATE);
-
- if(isSetPassword())
- {
- showLoginDialog();
- }
- else
- {
- showFirstDialog();
- }
- }
-
- private void showLoginDialog()
- {
- dialog = new Dialog(this, R.style.MyDialog);
- View view = View.inflate(this, R.layout.login_dialog, null);
- password = (EditText) view.findViewById(R.id.et_protected_password);
- Button yes = (Button) view.findViewById(R.id.bt_protected_login_yes);
- Button cancel = (Button) view.findViewById(R.id.bt_protected_login_no);
- yes.setOnClickListener(this);
- cancel.setOnClickListener(this);
- dialog.setContentView(view);
- dialog.show();
- }
- private void showFirstDialog()
- {
- dialog = new Dialog(this, R.style.MyDialog);
- //dialog.setContentView(R.layout.first_dialog);
- View view = View.inflate(this, R.layout.first_dialog, null); //這樣來填充一個而已文件,比較方便
- password = (EditText) view.findViewById(R.id.et_protected_first_password);
- confirmPassword = (EditText) view.findViewById(R.id.et_protected_confirm_password);
- Button yes = (Button) view.findViewById(R.id.bt_protected_first_yes);
- Button cancel = (Button) view.findViewById(R.id.bt_protected_first_no);
- yes.setOnClickListener(this);
- cancel.setOnClickListener(this);
- dialog.setContentView(view);
- dialog.show();
- }
- private boolean isSetPassword()
- {
- String pwd = sp.getString("password", "");
- if(pwd.equals("") || pwd == null)
- {
- return false;
- }
- return true;
- }
- @Override
- public void onClick(View v)
- {
- switch(v.getId())
- {
- case R.id.bt_protected_first_yes :
- String fp = password.getText().toString().trim();
- String cp = confirmPassword.getText().toString().trim();
- if(fp.equals("") || cp.equals(""))
- {
- Toast.makeText(this, "密碼不能為空", Toast.LENGTH_SHORT).show();
- return;
- }
- else
- {
- if(fp.equals(cp))
- {
- Editor editor = sp.edit();
- editor.putString("password", MD5Encoder.encode(fp));
- editor.commit();
- }
- else
- {
- Toast.makeText(this, "兩次密碼不相同", Toast.LENGTH_SHORT).show();
- return;
- }
- }
- dialog.dismiss();
- break;
-
- case R.id.bt_protected_first_no :
- dialog.dismiss();
- finish();
- break;
-
- case R.id.bt_protected_login_yes :
- String pwd = password.getText().toString().toString();
- if(pwd.equals(""))
- {
- Toast.makeText(this, "請輸入密碼", Toast.LENGTH_SHORT).show();
- }
- else
- {
- String str = sp.getString("password", "");
- if(MD5Encoder.encode(pwd).equals(str))
- {
- dialog.dismiss();
- }
- else
- {
- Toast.makeText(this, "密碼錯誤", Toast.LENGTH_SHORT).show();
- }
- }
- break;
-
- case R.id.bt_protected_login_no :
- dialog.dismiss();
- finish();
- break;
-
- default :
- break;
- }
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:andro
- 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:textColor="@android:color/white"
- android:textSize="24sp"
- android:text="@string/setPassword"/>
-
- <LinearLayout
- android:layout_width="300dip"
- android:layout_height="180dip"
- android:background="#ffc8c8c8"
- android:orientation="vertical">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/setPasswordDescription"/>
-
- <EditText
- android:
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:inputType="textPassword"/>
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/againPassword"/>
-
- <EditText
- android:
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:inputType="textPassword"/>
-
- <LinearLayout
- android:layout_width="300dip"
- android:layout_height="50dip"
- android:gravity="center"
- android:orientation="horizontal">
-
- <Button
- android:
- android:layout_width="140dip"
- android:layout_height="40dip"
- android:text="@string/protectedYes"/>
-
- <Button
- android:
- android:layout_width="140dip"
- android:layout_height="40dip"
- android:layout_marginLeft="10dip"
- android:text="@string/protectedNo"/>
-
- </LinearLayout>
-
- </LinearLayout>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:andro
- android:layout_width="300dip"
- android:layout_height="180dip"
- android:gravity="center_horizontal"
- android:orientation="vertical" >
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="@android:color/white"
- android:textSize="24sp"
- android:text="@string/login"/>
-
- <LinearLayout
- android:layout_width="300dip"
- android:layout_height="120dip"
- android:background="#ffc8c8c8"
- android:orientation="vertical">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/inputPassword"/>
-
- <EditText
- android:
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:inputType="textPassword"/>
-
- <LinearLayout
- android:layout_width="300dip"
- android:layout_height="50dip"
- android:gravity="center"
- android:orientation="horizontal">
-
- <Button
- android:
- android:layout_width="140dip"
- android:layout_height="40dip"
- android:text="@string/protectedYes"/>
-
- <Button
- android:
- android:layout_width="140dip"
- android:layout_height="40dip"
- android:layout_marginLeft="10dip"
- android:text="@string/protectedNo"/>
-
- </LinearLayout>
-
- </LinearLayout>
- </LinearLayout>

最後更新:2017-04-03 14:54:43
上一篇:
“爆發戶”小米憑啥那麼牛
下一篇:
[LeetCode]136.Single Numbe
【雲棲大會】聯想雲與阿裏雲攜手打造智能時代雲架構
Sql Server 存儲過程實例講解
Cmdb、Saltstack、Web化,莉莉絲遊戲雲上運維心得分享
帶進度條的webview
Ubuntu14.04安裝JDK
Pro JavaScript Techniques第一章: 現代javscript編程
《數據分析實戰:基於EXCEL和SPSS係列工具的實踐》一3.1 數據采集的幾條重要原則
hibernate操作java.util.Date和java.sql.Date
如何在 XenServer 7 GUI 虛擬機(VM)上提高屏幕分辨率
Hibernate之update(2)——報錯query must begin with SELECT or FROM