好啦,我们之前已经把设置向导的界面已经全部完成的了,而且界面也已经完成了三个的啦,今天我们把最后的一个界面完成它,还有把防盗的逻辑也完成一下
废话不多说,直接上代码 com.xiaobin.security.ui.SetupGuide4Activity
- package com.xiaobin.security.ui;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- 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.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- import com.xiaobin.security.R;
- public class SetupGuide4Activity extends Activity implements OnClickListener
- {
- private Button bt_pervious;
- private Button bt_finish;
- private CheckBox cb_protected;
- private SharedPreferences sp;
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.setup_guide4);
-
- bt_pervious = (Button) findViewById(R.id.bt_guide_pervious);
- bt_finish = (Button) findViewById(R.id.bt_guide_finish);
- bt_finish.setOnClickListener(this);
- bt_pervious.setOnClickListener(this);
-
- cb_protected = (CheckBox) findViewById(R.id.cb_guide_protected);
-
- sp = getSharedPreferences("config", Context.MODE_PRIVATE);
- boolean isProtecting = sp.getBoolean("isProtected", false);
- if(isProtecting)
- {
- cb_protected.setText("已经开启保护");
- cb_protected.setChecked(true);
- }
-
- cb_protected.setOnCheckedChangeListener(new OnCheckedChangeListener()
- {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
- {
- if(isChecked)
- {
- cb_protected.setText("已经开启保护");
- Editor editor = sp.edit();
- editor.putBoolean("isProtected", true);
- editor.commit();
- }
- else
- {
- cb_protected.setText("没有开启保护");
- Editor editor = sp.edit();
- editor.putBoolean("isProtected", false);
- editor.commit();
- }
- }
- });
- }
- @Override
- public void onClick(View v)
- {
- switch(v.getId())
- {
- case R.id.bt_guide_finish :
- if(cb_protected.isChecked())
- {
- Editor editor = sp.edit();
- editor.putBoolean("setupGuide", true);//记录是否已经进行过设置向导了
- editor.commit();
- finish();
- }
- else
- {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("提醒");
- builder.setMessage("强烈建议您开启保护, 是否完成设置");
- builder.setCancelable(false);
- builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
- {
- @Override
- public void onClick(DialogInterface dialog, int which)
- {
- Editor editor = sp.edit();
- editor.putBoolean("setupGuide", true);//记录是否已经进行过设置向导了
- editor.commit();
- finish();
- }
- });
- builder.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener()
- {
- @Override
- public void onClick(DialogInterface dialog, int which)
- {
- Editor editor = sp.edit();
- editor.putBoolean("setupGuide", true);//记录是否已经进行过设置向导了
- editor.commit();
- }
- });
- builder.create().show();
- }
- break;
-
- case R.id.bt_guide_pervious :
- Intent intent = new Intent(this, SetupGuide3Activity.class);
- finish();
- startActivity(intent);
- //这个是定义activity切换时的动画效果的
- overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
- break;
-
- default :
- break;
- }
- }
- }
复制代码
好啦,就这个样子,我们的设置向导就全部完成的啦,现在我们就要来写完我们的防盗主界面啦,lost_protected.xml
- <?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:id="@+id/tv_lost_protected_number"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="5dip"
- android:text="@string/lostProtectedNumber"
- android:textSize="20sp"/>
-
- <View
- style="@style/SetGuideUnderline"/>
-
- <CheckBox
- android:id="@+id/cb_lost_protected_isProtected"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/isProtected"
- android:textSize="20sp"/>
-
- <View
- style="@style/SetGuideUnderline"/>
-
- <TextView
- android:id="@+id/tv_lost_protected_guide"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/rePlayGuide"
- android:textSize="20sp"/>
- </LinearLayout>
复制代码
com.xiaobin.security.ui.LostProtectedActivit
- package com.xiaobin.security.ui;
- import android.app.Activity;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.Intent;
- 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.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- 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;
- private TextView tv_protectedNumber;
- private TextView tv_protectedGuide;
- private CheckBox cb_isProtected;
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
-
- sp = getSharedPreferences("config", 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.setCancelable(false);
- 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);//这种填充布局的方式比较方便,峭用拿到一个LayoutInflate对象
- 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.setCancelable(false);
- dialog.show();
- }
- private boolean isSetPassword()
- {
- String pwd = sp.getString("password", "");
- if(pwd.equals("") || pwd == null)
- {
- return false;
- }
- return true;
- }
-
- private boolean isSetupGuide()
- {
- return sp.getBoolean("setupGuide", false);
- }
- @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();
- dialog.dismiss();
-
- if(!isSetupGuide())
- {
- finish();
- Intent intent = new Intent(this, SetupGuide1Activity.class);
- startActivity(intent);
- }
- }
- 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))
- {
- if(isSetupGuide())
- {
- setContentView(R.layout.lost_protected);
- tv_protectedNumber = (TextView) findViewById(R.id.tv_lost_protected_number);
- tv_protectedGuide = (TextView) findViewById(R.id.tv_lost_protected_guide);
- cb_isProtected = (CheckBox) findViewById(R.id.cb_lost_protected_isProtected);
-
- tv_protectedNumber.setText("手机安全号码为:" + sp.getString("number", ""));
- tv_protectedGuide.setOnClickListener(this);
-
- boolean isProtecting = sp.getBoolean("isProtected", false);
- if(isProtecting)
- {
- cb_isProtected.setText("已经开启保护");
- cb_isProtected.setChecked(true);
- }
-
- cb_isProtected.setOnCheckedChangeListener(new OnCheckedChangeListener()
- {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
- {
- if(isChecked)
- {
- cb_isProtected.setText("已经开启保护");
- Editor editor = sp.edit();
- editor.putBoolean("isProtected", true);
- editor.commit();
- }
- else
- {
- cb_isProtected.setText("没有开启保护");
- Editor editor = sp.edit();
- editor.putBoolean("isProtected", false);
- editor.commit();
- }
- }
- });
- }
- dialog.dismiss();
- }
- else
- {
- Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show();
- }
- }
- break;
-
- case R.id.bt_protected_login_no :
- dialog.dismiss();
- finish();
- break;
-
- case R.id.tv_lost_protected_guide : //重新进入设置向导
- finish();
- Intent setupGuideIntent = new Intent(this, SetupGuide1Activity.class);
- startActivity(setupGuideIntent);
- break;
-
- default :
- break;
- }
- }
- }
复制代码
- com.xiaobin.security.ui.<span style="font-family:Arial, Helvetica, sans-serif;">SetupGuide2Activity </span>
复制代码
好啦,到现在为止,我们的手机防盗这个功能的界面就基本上完成的啦,那么现在就进入我们的重头戏啦,就是防盗逻辑的完成啦,我们现在的防盗是这样的,手机丢失的时候,小偷肯定是会换sim卡的嘛,所以就要重启啦,那我们就在启动完成之后,比较一下现在的sim卡,是不是我们之前设置保护的sim卡,如果不是,那就发送一条短信到已经设置了的安全号码那里。所以现在我们只要写一个广播接收者,把手机重启的广播接收到,然后再起进行判断就行啦好啦,现在直接上代码com.xiaobin.security.receiver.BootCompleteReceiver
- package com.xiaobin.security.receiver;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.telephony.SmsManager;
- import android.telephony.TelephonyManager;
- public class BootCompleteReceiver extends BroadcastReceiver
- {
- private SharedPreferences sp;
- @Override
- public void onReceive(Context context, Intent intent)
- {
- sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
- boolean isProtected = sp.getBoolean("isProtected", false);
- //看看是不是开启了保护
- if(isProtected)
- {
- TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
- //开机后,拿到当前sim卡的标识,与我们之前存放的标识对比
- String currentSim = telephonyManager.getSimSerialNumber();
- String protectedSim = sp.getString("simSerial", "");
- if(!currentSim.equals(protectedSim))
- {
- //拿到一个短信的管理器,要注意不要导错包,是在android.telephony下的
- SmsManager smsManager = SmsManager.getDefault();
- String number = sp.getString("number", "");
- //发送短信,有5个参数,第一个是要发送到的地址,第二个是发送人,可以设置为null,第三个是要发送的信息,第四个是发送状态,第五个是发送后的,都可以置为null
- smsManager.sendTextMessage(number, null, "Sim卡已经变更了,手机可能被盗", null, null);
- }
- }
- }
- }
复制代码
好啦,现在我们就要在AndroidManifest文件里面注册这个广播接收者啦
- <receiver
- android:name="com.xiaobin.security.receiver.BootCompleteReceiver">
- <intent-filter android:priority="1000">
- <action android:name="android.intent.action.BOOT_COMPLETED"/><!-- 这个是开机完成后的广播 -->
- </intent-filter>
- </receiver>
复制代码
当然,现在还不行的,我们还要把相应的权限加上
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
- <uses-permission android:name="android.permission.SEND_SMS"/>
复制代码
好啦,现在我们的逻辑就基本上完成的啦但现在这个功能还不是很完整,所以我们后续的课程将会继续完善这个防盗的功能的,比如加入gps定位啦,这些的。 好,今天的代码就到这里了,如果有什么不明白的,和指导的,欢迎留言!,明天将会完善这个防盗功能,加入gps! Security_09防盗逻辑以及设置向导的完成.rar (233.32 KB, 下载次数: 50) |