閱讀307 返回首頁    go 京東網上商城


手機衛士09-防盜邏輯實現



好啦,我們之前已經把設置向導的界麵已經全部完成的了,而且界麵也已經完成了三個的啦,今天我們把最後的一個界麵完成它,還有把防盜的邏輯也完成一下


廢話不多說,直接上代碼

com.xiaobin.security.ui.SetupGuide4Activity

  1. package com.xiaobin.security.ui;

  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.content.SharedPreferences.Editor;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. import android.widget.CheckBox;
  14. import android.widget.CompoundButton;
  15. import android.widget.CompoundButton.OnCheckedChangeListener;

  16. import com.xiaobin.security.R;

  17. public class SetupGuide4Activity extends Activity implements OnClickListener
  18. {
  19.         private Button bt_pervious;
  20.         private Button bt_finish;
  21.         private CheckBox cb_protected;
  22.         private SharedPreferences sp;
  23.         
  24.         @Override
  25.         protected void onCreate(Bundle savedInstanceState)
  26.         {
  27.                 super.onCreate(savedInstanceState);
  28.                 setContentView(R.layout.setup_guide4);
  29.                 
  30.                 bt_pervious = (Button) findViewById(R.id.bt_guide_pervious);
  31.                 bt_finish = (Button) findViewById(R.id.bt_guide_finish);
  32.                 bt_finish.setOnClickListener(this);
  33.                 bt_pervious.setOnClickListener(this);
  34.                 
  35.                 cb_protected = (CheckBox) findViewById(R.id.cb_guide_protected);
  36.                 
  37.                 sp = getSharedPreferences("config", Context.MODE_PRIVATE);
  38.                 boolean isProtecting = sp.getBoolean("isProtected", false);
  39.                 if(isProtecting)
  40.                 {
  41.                         cb_protected.setText("已經開啟保護");
  42.                         cb_protected.setChecked(true);
  43.                 }
  44.                 
  45.                 cb_protected.setOnCheckedChangeListener(new OnCheckedChangeListener()
  46.                 {
  47.                         @Override
  48.                         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
  49.                         {
  50.                                 if(isChecked)
  51.                                 {
  52.                                         cb_protected.setText("已經開啟保護");
  53.                                         Editor editor = sp.edit();
  54.                                         editor.putBoolean("isProtected", true);
  55.                                         editor.commit();
  56.                                 }
  57.                                 else
  58.                                 {
  59.                                         cb_protected.setText("沒有開啟保護");
  60.                                         Editor editor = sp.edit();
  61.                                         editor.putBoolean("isProtected", false);
  62.                                         editor.commit();
  63.                                 }
  64.                         }
  65.                 });
  66.         }

  67.         @Override
  68.         public void onClick(View v)
  69.         {
  70.                 switch(v.getId())
  71.                 {
  72.                         case R.id.bt_guide_finish : 
  73.                                 if(cb_protected.isChecked())
  74.                                 {
  75.                                         Editor editor = sp.edit();
  76.                                         editor.putBoolean("setupGuide", true);//記錄是否已經進行過設置向導了
  77.                                         editor.commit();
  78.                                         finish();
  79.                                 }
  80.                                 else
  81.                                 {
  82.                                         AlertDialog.Builder builder = new AlertDialog.Builder(this);
  83.                                         builder.setTitle("提醒");
  84.                                         builder.setMessage("強烈建議您開啟保護, 是否完成設置");
  85.                                         builder.setCancelable(false);
  86.                                         builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
  87.                                         {
  88.                                                 @Override
  89.                                                 public void onClick(DialogInterface dialog, int which)
  90.                                                 {
  91.                                                         Editor editor = sp.edit();
  92.                                                         editor.putBoolean("setupGuide", true);//記錄是否已經進行過設置向導了
  93.                                                         editor.commit();
  94.                                                         finish();
  95.                                                 }
  96.                                         });
  97.                                         builder.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener()
  98.                                         {
  99.                                                 @Override
  100.                                                 public void onClick(DialogInterface dialog, int which)
  101.                                                 {
  102.                                                         Editor editor = sp.edit();
  103.                                                         editor.putBoolean("setupGuide", true);//記錄是否已經進行過設置向導了
  104.                                                         editor.commit();
  105.                                                 }
  106.                                         });
  107.                                         builder.create().show();
  108.                                 }
  109.                                 break;
  110.                                 
  111.                         case R.id.bt_guide_pervious : 
  112.                                 Intent intent = new Intent(this, SetupGuide3Activity.class);
  113.                                 finish();
  114.                                 startActivity(intent);
  115.                                 //這個是定義activity切換時的動畫效果的
  116.                                 overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
  117.                                 break;
  118.                                 
  119.                         default : 
  120.                                 break;
  121.                 }
  122.         }

  123. }
複製代碼
好啦,就這個樣子,我們的設置向導就全部完成的啦,現在我們就要來寫完我們的防盜主界麵啦,lost_protected.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:andro
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical" >
  6.     
  7.     <TextView 
  8.         android:
  9.         android:layout_width="match_parent"
  10.         android:layout_height="wrap_content"
  11.         android:layout_marginTop="5dip"
  12.         android:text="@string/lostProtectedNumber"
  13.         android:textSize="20sp"/>
  14.     
  15.     <View 
  16.         />
  17.     
  18.     <CheckBox 
  19.         android:
  20.         android:layout_width="wrap_content"
  21.         android:layout_height="wrap_content"
  22.         android:text="@string/isProtected"
  23.         android:textSize="20sp"/>
  24.     
  25.     <View 
  26.         />
  27.     
  28.     <TextView 
  29.         android:
  30.         android:layout_width="match_parent"
  31.         android:layout_height="wrap_content"
  32.         android:text="@string/rePlayGuide"
  33.         android:textSize="20sp"/>

  34. </LinearLayout>
複製代碼
com.xiaobin.security.ui.LostProtectedActivit
  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.Intent;
  6. import android.content.SharedPreferences;
  7. import android.content.SharedPreferences.Editor;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import android.widget.CheckBox;
  13. import android.widget.CompoundButton;
  14. import android.widget.EditText;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17. import android.widget.CompoundButton.OnCheckedChangeListener;

  18. import com.xiaobin.security.R;
  19. import com.xiaobin.security.utils.MD5Encoder;

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

  60.         private void showFirstDialog()
  61.         {
  62.                 dialog = new Dialog(this, R.style.MyDialog);
  63.                 //dialog.setContentView(R.layout.first_dialog);
  64.                 View view = View.inflate(this, R.layout.first_dialog, null);//這種填充布局的方式比較方便,峭用拿到一個LayoutInflate對象
  65.                 password = (EditText) view.findViewById(R.id.et_protected_first_password);
  66.                 confirmPassword = (EditText) view.findViewById(R.id.et_protected_confirm_password);
  67.                 Button yes = (Button) view.findViewById(R.id.bt_protected_first_yes);
  68.                 Button cancel = (Button) view.findViewById(R.id.bt_protected_first_no);
  69.                 yes.setOnClickListener(this);
  70.                 cancel.setOnClickListener(this);
  71.                 dialog.setContentView(view);
  72.                 dialog.setCancelable(false);
  73.                 dialog.show();
  74.         }

  75.         private boolean isSetPassword()
  76.         {
  77.                 String pwd = sp.getString("password", "");
  78.                 if(pwd.equals("") || pwd == null)
  79.                 {
  80.                         return false;
  81.                 }
  82.                 return true;
  83.         }
  84.         
  85.         private boolean isSetupGuide()
  86.         {
  87.                 return sp.getBoolean("setupGuide", false);
  88.         }

  89.         @Override
  90.         public void onClick(View v)
  91.         {
  92.                 switch(v.getId())
  93.                 {
  94.                         case R.id.bt_protected_first_yes : 
  95.                                 String fp = password.getText().toString().trim();
  96.                                 String cp = confirmPassword.getText().toString().trim();
  97.                                 if(fp.equals("") || cp.equals(""))
  98.                                 {
  99.                                         Toast.makeText(this, "密碼不能為空", Toast.LENGTH_SHORT).show();
  100.                                         return;
  101.                                 }
  102.                                 else 
  103.                                 {
  104.                                         if(fp.equals(cp))
  105.                                         {
  106.                                                 Editor editor = sp.edit();
  107.                                                 editor.putString("password", MD5Encoder.encode(fp));
  108.                                                 editor.commit();
  109.                                                 dialog.dismiss();
  110.                                                 
  111.                                                 if(!isSetupGuide())
  112.                                                 {
  113.                                                         finish();
  114.                                                         Intent intent = new Intent(this, SetupGuide1Activity.class);
  115.                                                         startActivity(intent);
  116.                                                 }
  117.                                         }
  118.                                         else
  119.                                         {
  120.                                                 Toast.makeText(this, "兩次密碼不相同", Toast.LENGTH_SHORT).show();
  121.                                                 return;
  122.                                         }
  123.                                 }
  124.                                 dialog.dismiss();
  125.                                 break;
  126.                                 
  127.                         case R.id.bt_protected_first_no : 
  128.                                 dialog.dismiss();
  129.                                 finish();
  130.                                 break;
  131.                                 
  132.                         case R.id.bt_protected_login_yes : 
  133.                                 String pwd = password.getText().toString().toString();
  134.                                 if(pwd.equals(""))
  135.                                 {
  136.                                         Toast.makeText(this, "請輸入密碼", Toast.LENGTH_SHORT).show();
  137.                                 }
  138.                                 else
  139.                                 {
  140.                                         String str = sp.getString("password", "");
  141.                                         if(MD5Encoder.encode(pwd).equals(str))
  142.                                         {
  143.                                                 if(isSetupGuide())
  144.                                                 {
  145.                                                         setContentView(R.layout.lost_protected);
  146.                                                         tv_protectedNumber = (TextView) findViewById(R.id.tv_lost_protected_number);
  147.                                                         tv_protectedGuide = (TextView) findViewById(R.id.tv_lost_protected_guide);
  148.                                                         cb_isProtected = (CheckBox) findViewById(R.id.cb_lost_protected_isProtected);
  149.                                                         
  150.                                                         tv_protectedNumber.setText("手機安全號碼為:" + sp.getString("number", ""));
  151.                                                         tv_protectedGuide.setOnClickListener(this);
  152.                                                         
  153.                                                         boolean isProtecting = sp.getBoolean("isProtected", false);
  154.                                                         if(isProtecting)
  155.                                                         {
  156.                                                                 cb_isProtected.setText("已經開啟保護");
  157.                                                                 cb_isProtected.setChecked(true);
  158.                                                         }
  159.                                                         
  160.                                                         cb_isProtected.setOnCheckedChangeListener(new OnCheckedChangeListener()
  161.                                                         {
  162.                                                                 @Override
  163.                                                                 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
  164.                                                                 {
  165.                                                                         if(isChecked)
  166.                                                                         {
  167.                                                                                 cb_isProtected.setText("已經開啟保護");
  168.                                                                                 Editor editor = sp.edit();
  169.                                                                                 editor.putBoolean("isProtected", true);
  170.                                                                                 editor.commit();
  171.                                                                         }
  172.                                                                         else
  173.                                                                         {
  174.                                                                                 cb_isProtected.setText("沒有開啟保護");
  175.                                                                                 Editor editor = sp.edit();
  176.                                                                                 editor.putBoolean("isProtected", false);
  177.                                                                                 editor.commit();
  178.                                                                         }
  179.                                                                 }
  180.                                                         });
  181.                                                 }
  182.                                                 dialog.dismiss();
  183.                                         }
  184.                                         else
  185.                                         {
  186.                                                 Toast.makeText(this, "密碼錯誤", Toast.LENGTH_SHORT).show();
  187.                                         }
  188.                                 }
  189.                                 break;
  190.                                 
  191.                         case R.id.bt_protected_login_no : 
  192.                                 dialog.dismiss();
  193.                                 finish();
  194.                                 break;
  195.                                 
  196.                         case R.id.tv_lost_protected_guide : //重新進入設置向導
  197.                                 finish();
  198.                                 Intent setupGuideIntent = new Intent(this, SetupGuide1Activity.class);
  199.                                 startActivity(setupGuideIntent);
  200.                                 break;
  201.                                 
  202.                         default : 
  203.                                 break;
  204.                 }
  205.         }

  206. }
複製代碼
  1. com.xiaobin.security.ui.<span >SetupGuide2Activity </span>
複製代碼
  1. package com.xiaobin.security.ui;

  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.content.SharedPreferences.Editor;
  7. import android.os.Bundle;
  8. import android.telephony.TelephonyManager;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import android.widget.CheckBox;
  13. import android.widget.CompoundButton;
  14. import android.widget.CompoundButton.OnCheckedChangeListener;

  15. import com.xiaobin.security.R;

  16. public class SetupGuide2Activity extends Activity implements OnClickListener
  17. {
  18.         private Button bt_bind;
  19.         private Button bt_next;
  20.         private Button bt_pervious;
  21.         private CheckBox cb_bind;
  22.         private SharedPreferences sp;
  23.         
  24.         @Override
  25.         protected void onCreate(Bundle savedInstanceState)
  26.         {
  27.                 super.onCreate(savedInstanceState);
  28.                 setContentView(R.layout.setup_guide2);
  29.                 
  30.                 sp = getSharedPreferences("config", Context.MODE_PRIVATE);
  31.                 
  32.                 bt_bind = (Button) findViewById(R.id.bt_guide_bind);
  33.                 bt_next = (Button) findViewById(R.id.bt_guide_next);
  34.                 bt_pervious = (Button) findViewById(R.id.bt_guide_pervious);
  35.                 bt_bind.setOnClickListener(this);
  36.                 bt_next.setOnClickListener(this);
  37.                 bt_pervious.setOnClickListener(this);
  38.                 
  39.                 cb_bind = (CheckBox) findViewById(R.id.cb_guide_check);
  40.                 //初始化CheckBox狀態
  41.                 String sim = sp.getString("simSerial", null);
  42.                 if(sim != null)
  43.                 {
  44.                         cb_bind.setText("已經綁定");
  45.                         cb_bind.setChecked(true);
  46.                 }
  47.                 else
  48.                 {
  49.                         cb_bind.setText("沒有綁定");
  50.                         cb_bind.setChecked(false);
  51.                         resetSimInfo();
  52.                 }
  53.                 cb_bind.setOnCheckedChangeListener(new OnCheckedChangeListener()
  54.                 {
  55.                         @Override
  56.                         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
  57.                         {
  58.                                 if(isChecked)
  59.                                 {
  60.                                         cb_bind.setText("已經綁定");
  61.                                         setSimInfo();
  62.                                 }
  63.                                 else
  64.                                 {
  65.                                         cb_bind.setText("沒有綁定");
  66.                                         resetSimInfo();
  67.                                 }
  68.                         }
  69.                 });
  70.         }

  71.         @Override
  72.         public void onClick(View v)
  73.         {
  74.                 switch(v.getId())
  75.                 {
  76.                         case R.id.bt_guide_bind : 
  77.                                 setSimInfo();
  78.                                 cb_bind.setText("已經綁定");
  79.                                 cb_bind.setChecked(true);
  80.                                 break;
  81.                                 
  82.                         case R.id.bt_guide_next : 
  83.                                 Intent intent = new Intent(this, SetupGuide3Activity.class);
  84.                                 finish();
  85.                                 startActivity(intent);
  86.                                 //這個是定義activity切換時的動畫效果的
  87.                                 overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
  88.                                 break;
  89.                         case R.id.bt_guide_pervious : 
  90.                                 
  91.                                 Intent i = new Intent(this, SetupGuide1Activity.class);
  92.                                 finish();
  93.                                 startActivity(i);
  94.                                 //這個是定義activity切換時的動畫效果的
  95.                                 overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
  96.                                 break;
  97.                                 
  98.                         default : 
  99.                                 break;
  100.                 }
  101.         }
  102.         
  103.         private void setSimInfo()
  104.         {
  105.                 TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
  106.                 String simSerial = telephonyManager.getSimSerialNumber();//拿到sim卡的序列號,是唯一的
  107.                 Editor editor = sp.edit();
  108.                 editor.putString("simSerial", simSerial);
  109.                 editor.commit();
  110.         }
  111.         
  112.         private void resetSimInfo()        //解除綁定
  113.         {
  114.                 Editor editor = sp.edit();
  115.                 editor.putString("simSerial", null);
  116.                 editor.commit();
  117.         }

  118. }
複製代碼
好啦,到現在為止,我們的手機防盜這個功能的界麵就基本上完成的啦,那麼現在就進入我們的重頭戲啦,就是防盜邏輯的完成啦,我們現在的防盜是這樣的,手機丟失的時候,小偷肯定是會換sim卡的嘛,所以就要重啟啦,那我們就在啟動完成之後,比較一下現在的sim卡,是不是我們之前設置保護的sim卡,如果不是,那就發送一條短信到已經設置了的安全號碼那裏。所以現在我們隻要寫一個廣播接收者,把手機重啟的廣播接收到,然後再起進行判斷就行啦好啦,現在直接上代碼com.xiaobin.security.receiver.BootCompleteReceiver
  1. package com.xiaobin.security.receiver;

  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.telephony.SmsManager;
  7. import android.telephony.TelephonyManager;

  8. public class BootCompleteReceiver extends BroadcastReceiver
  9. {
  10.         private SharedPreferences sp;

  11.         @Override
  12.         public void onReceive(Context context, Intent intent)
  13.         {
  14.                 sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
  15.                 boolean isProtected = sp.getBoolean("isProtected", false);
  16.                 //看看是不是開啟了保護
  17.                 if(isProtected)
  18.                 {
  19.                         TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  20.                         //開機後,拿到當前sim卡的標識,與我們之前存放的標識對比
  21.                         String currentSim = telephonyManager.getSimSerialNumber();
  22.                         String protectedSim = sp.getString("simSerial", "");
  23.                         if(!currentSim.equals(protectedSim))
  24.                         {
  25.                                 //拿到一個短信的管理器,要注意不要導錯包,是在android.telephony下的
  26.                                 SmsManager smsManager = SmsManager.getDefault();
  27.                                 String number = sp.getString("number", "");
  28.                                 //發送短信,有5個參數,第一個是要發送到的地址,第二個是發送人,可以設置為null,第三個是要發送的信息,第四個是發送狀態,第五個是發送後的,都可以置為null
  29.                                 smsManager.sendTextMessage(number, null, "Sim卡已經變更了,手機可能被盜", null, null);
  30.                         }
  31.                 }
  32.         }

  33. }
複製代碼
好啦,現在我們就要在AndroidManifest文件裏麵注冊這個廣播接收者啦
  1.         <receiver 
  2.             android:name="com.xiaobin.security.receiver.BootCompleteReceiver">
  3.             <intent-filter android:priority="1000">
  4.                 <action android:name="android.intent.action.BOOT_COMPLETED"/><!-- 這個是開機完成後的廣播 -->
  5.             </intent-filter>
  6.         </receiver>
複製代碼
當然,現在還不行的,我們還要把相應的權限加上
  1.     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
  2.     <uses-permission android:name="android.permission.SEND_SMS"/>
複製代碼
好啦,現在我們的邏輯就基本上完成的啦但現在這個功能還不是很完整,所以我們後續的課程將會繼續完善這個防盜的功能的,比如加入gps定位啦,這些的。 好,今天的代碼就到這裏了,如果有什麼不明白的,和指導的,歡迎留言!,明天將會完善這個防盜功能,加入gps!  Security_09防盜邏輯以及設置向導的完成.rar (233.32 KB, 下載次數: 50) 

最後更新:2017-04-03 12:53:35

  上一篇:go C++對象模型(二):The Semantics of Copy Constructors(拷貝構造函數之編譯背後的行為)
  下一篇:go C++編程規範之11:隱藏信息