好啦,我们之前已经把设置向导的界面已经全部完成的了,而且界面也已经完成了三个的啦,今天我们把最后的一个界面完成它,还有把防盗的逻辑也完成一下
废话不多说,直接上代码
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;
- }
- }
- }
- 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.SetupGuide2Activity
- package com.xiaobin.security.ui;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.content.SharedPreferences.Editor;
- import android.os.Bundle;
- import android.telephony.TelephonyManager;
- 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 SetupGuide2Activity extends Activity implements OnClickListener
- {
- private Button bt_bind;
- private Button bt_next;
- private Button bt_pervious;
- private CheckBox cb_bind;
- private SharedPreferences sp;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.setup_guide2);
- sp = getSharedPreferences("config", Context.MODE_PRIVATE);
- bt_bind = (Button) findViewById(R.id.bt_guide_bind);
- bt_next = (Button) findViewById(R.id.bt_guide_next);
- bt_pervious = (Button) findViewById(R.id.bt_guide_pervious);
- bt_bind.setOnClickListener(this);
- bt_next.setOnClickListener(this);
- bt_pervious.setOnClickListener(this);
- cb_bind = (CheckBox) findViewById(R.id.cb_guide_check);
- //初始化CheckBox状态
- String sim = sp.getString("simSerial", null);
- if(sim != null)
- {
- cb_bind.setText("已经绑定");
- cb_bind.setChecked(true);
- }
- else
- {
- cb_bind.setText("没有绑定");
- cb_bind.setChecked(false);
- resetSimInfo();
- }
- cb_bind.setOnCheckedChangeListener(new OnCheckedChangeListener()
- {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
- {
- if(isChecked)
- {
- cb_bind.setText("已经绑定");
- setSimInfo();
- }
- else
- {
- cb_bind.setText("没有绑定");
- resetSimInfo();
- }
- }
- });
- }
- @Override
- public void onClick(View v)
- {
- switch(v.getId())
- {
- case R.id.bt_guide_bind :
- setSimInfo();
- cb_bind.setText("已经绑定");
- cb_bind.setChecked(true);
- break;
- case R.id.bt_guide_next :
- Intent intent = new Intent(this, SetupGuide3Activity.class);
- finish();
- startActivity(intent);
- //这个是定义activity切换时的动画效果的
- overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
- break;
- case R.id.bt_guide_pervious :
- Intent i = new Intent(this, SetupGuide1Activity.class);
- finish();
- startActivity(i);
- //这个是定义activity切换时的动画效果的
- overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
- break;
- default :
- break;
- }
- }
- private void setSimInfo()
- {
- TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
- String simSerial = telephonyManager.getSimSerialNumber();//拿到sim卡的序列号,是唯一的
- Editor editor = sp.edit();
- editor.putString("simSerial", simSerial);
- editor.commit();
- }
- private void resetSimInfo() //解除绑定
- {
- Editor editor = sp.edit();
- editor.putString("simSerial", null);
- editor.commit();
- }
- }
- 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);
- }
- }
- }
- }
