手机防盗软件是现在主流杀毒软件都会有的一个基本功能, 这其中会涉及到一些都手机内部的简单了解 ,本人其中有一些小小的领悟 拿出来分享 共同学习
先上图:
当用户第一次进入 读取首选项文件密码是否为空 ,如为空 就需要用户设置密码 ,否者直接输入密码
// 获取首先项中的密码
public Boolean isEntry() {
// 获取首选项中的password字段
String password = pf.getString("password", "");
//System.out.println(password);
// 返回的布尔值
return password.isEmpty();
}
// 判断手机是否设置过密码;
if (isEntry()) {
// 第一次进入
showDialog();
} else {
// 正常进入
showNormalEntryDialog();
}
对话框设置密码
// 使用对话框进行密码设置
public void showDialog() {
// 创建对话框对象
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("设置密码");
// 获取布局文件
View viewFrist = View.inflate(this, R.layout.lostprotectedactivity,
null);
lost_editvp = (EditText) viewFrist.findViewById(R.id.lost_editvp);
lost_vpsw = (EditText) viewFrist.findViewById(R.id.lost_editpsw);
// 将布局文件设置到对话框中
builder.setView(viewFrist);
builder.setCancelable(false);
// 设置按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点击确认密码时获取密码和确认密码
String pwd = lost_editvp.getText().toString().trim();
String pwds = lost_vpsw.getText().toString().trim();
// Toast.makeText(LostProtectedActivity.this,
// "密码:" + pwd + "<><>确认密码:" + pwds, 0).show();
// 密码或者确认密码为空
if (pwd.isEmpty() || pwds.isEmpty()) {
Toast.makeText(LostProtectedActivity.this, "不能为空", 0)
.show();
finish();
return;
}
// 密码和确认密码长度6位以上
if (pwd.length() < 6 || pwds.length() < 6) {
Toast.makeText(LostProtectedActivity.this, "请设置6位数以上的密码", 0)
.show();
finish();
return;
}
// 密码和确认密码不相等
if (!pwd.equals(pwds)) {
Toast.makeText(LostProtectedActivity.this, "密码不一致", 0)
.show();
finish();
return;
}
// 创建首选项的编辑对象
SharedPreferences.Editor pedit = pf.edit();
pedit.putString("password", pwds);
pedit.commit();// 提交
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点击取消按钮时关闭手机设置页面,返回到主页
finish();
}
});
// 显示
builder.create().show();
}
2,用户设置成功后会需要设置安全号码和是否开启防盗
// 点击事件获取安全号码,开启防盗
public void imgChange(View v) {
// Toast.makeText(this, "点击", 1).show();
if (v.getId() == R.id.loat_offandon) {
// 获得安全号码
String safephone = edithint.getText().toString().trim();
// 判断是否选中开启防盗
Boolean b = checkbox.isChecked();
if (safephone.length() == 11) {
if (b) {
// 设置权限
ComponentName adminName = new ComponentName(
LostProtectedActivity.this, MyAdminService.class);
// 获得设备的相关权限
DevicePolicyManager deviceMAnager = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
// 判断组件是否获得超级管理员权限
//if (deviceMAnager.isAdminActive(adminName)) {
// 使用Intent进行跳转
Intent intentAdmin = new Intent(
DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
// 将组件激活
// 添加一个隐式意图,完成设备权限的添加
// 这个Intent (DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)跳转到
// 权限提醒页面
// 并传递了两个参数EXTRA_DEVICE_ADMIN 、 EXTRA_ADD_EXPLANATION
intentAdmin.putExtra(
DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminName);
// 描述(additional explanation)
// EXTRA_ADD_EXPLANATION参数为附加的说明
intentAdmin.putExtra(
DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"百合不是茶 ");
// 跳转
startActivityForResult(intentAdmin, 0);
//}
// 动态的修改图片
loat_img.setImageDrawable(getResources().getDrawable(
R.drawable.av_widget_switch_on_normal));
// 创建首选项的编辑对象
SharedPreferences.Editor pedit = pf.edit();
pedit.putString("safephone", safephone);
pedit.putBoolean("ischecked", true);
pedit.putInt("loat_img", 2);
pedit.putString("simserial", getSimSerial());
// 改变图片
pedit.commit();
// 提交文本后设置不能再点击
loat_img.setClickable(false);
} else {
Toast.makeText(this, "请选择开启防盗 ", Toast.LENGTH_SHORT).show();
}
} else {
// 号码错误
Toast.makeText(this, "只支持手机号码 ", Toast.LENGTH_SHORT).show();
}
}
}
3,正常启动的方法
// 正常启动的方法
public void showNormalEntryDialog() {
// 创建对话框
AlertDialog.Builder builders = new AlertDialog.Builder(this);
View view = View.inflate(this, R.layout.lostpwd, null);
builders.setTitle("输入密码");
// 获取密码框
final EditText pwdedit = (EditText) view.findViewById(R.id.lostpwd_pwd);
builders.setView(view);
builders.setCancelable(false);
builders.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 获取输入的密码
String repwd = pwdedit.getText().toString().trim();
// 获取首选项中的password字段
String password = pf.getString("password", "");
// Log.i("BB", password);
if (repwd.length() != 6 || !password.equals(repwd)) {
finish();// 关闭
}
if (password.equals(repwd)) {
if (password.length() == repwd.length()) {
// 读取保存的信息
String safephone = pf.getString("safephone", "");
int img = pf.getInt("loat_img", 0);
Boolean c = pf.getBoolean("ischecked", false);
if (safephone != null) {
edithint.setText(safephone);
}
if (c) {
checkbox.setChecked(true);
}
if (img == 2) {
loat_img.setImageDrawable(getResources()
.getDrawable(
R.drawable.av_widget_switch_on_normal));
}
} else {
finish();// 关闭
}
}
}
});
builders.create().show();
}
4,使用开机广播判断当前本机号码是否改变
/**
*
* @author Administrator 广播类,监听开机等操作
*/
public class LostBroadCastReceive extends BroadcastReceiver {
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
// Intent it = new Intent(context, lostServerSms.class);
// context.startService(it);
Log.i("BB", "手机重启了");
// 创建首选项
SharedPreferences pf = context.getSharedPreferences("lostpwds",
context.MODE_PRIVATE);
// 获得是否开启防盗
Boolean c = pf.getBoolean("ischecked", false);
if (c) {
// 如果开启就获取开启时存入的sim卡号
String simserial = pf.getString("simserial", "");
// 获取开机时获取到的sim卡号
TelephonyManager reSimManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
String number = reSimManager.getSimSerialNumber();
// 如果号码不一样就发送短信给安全号码进行相应的操作
if (!simserial.equals(number)) {
// 获取安全号码
String safephone = pf.getString("safephone", "");
// 使用短信管理者发送短信
SmsManager smsSend = SmsManager.getDefault();
//使用短信管理者给安全号码发送短信
smsSend.sendTextMessage(safephone, null, "卡号已经改变:" + number,
null, null);
}
}
}
}
配置权限
<uses-permission android:name="android.permission.SEND_SMS" />
清单注册
<receiver android:name="com.wj.againstburglars.LostBroadCastReceive" >
<intent-filter android:priority="1000" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
4,位置,报警,锁屏,手机初始化相关实现由于拦截不到短信 所以不能做相关的实验 ,代码会上传 以供参考