手机防盗

手机防盗软件是现在主流杀毒软件都会有的一个基本功能,  这其中会涉及到一些都手机内部的简单了解 ,本人其中有一些小小的领悟 拿出来分享 共同学习

 

先上图:



 

 

当用户第一次进入 读取首选项文件密码是否为空 ,如为空 就需要用户设置密码 ,否者直接输入密码

// 获取首先项中的密码
	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,位置,报警,锁屏,手机初始化相关实现由于拦截不到短信 所以不能做相关的实验 ,代码会上传 以供参考

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
http://blog.csdn.net/xiaoxiao108/archive/2011/05/01/6381414.aspx 前段时间母亲手机遭贼了,以防万一,如果自己手机丢了,肯定会更郁闷,记得很多手机防盗功能,如果更换了sim卡就会,手机就会自动把新的 sim卡手机号,gps坐标,什么的发送到绑定的手机上。网上查了下资料,这类这类软件也挺多的。看了看功能也不是很复杂,就自己写了个玩玩 。 开发环境 vs2008 wm6 .net cf 3.5 编译运行代码时,电脑必须安装 Windows Mobile 6 Professional SDK Refresh.msi 实现方法很简单 1.每一个sim都有唯一的一个IMSI编号,可以根据IMSI编号来判断手机是否更换sim卡 2.如果检测到IMSI不是自己的sim卡的,可以确定其他人可能在用你的手机。 3.每次开机程序自动运行,检测到别人如果使用你的手机,自动把他的通话记录,跟gps坐标发送到绑定好的手机号上。 4.知道用你手机人的手机号,最近通话记录,gps坐标后,再自己想办法找到这人吧。 具体代码 1.取sim卡IMSI编号 使用 TapiLib.dll类库中的ControlTapi.GetIMSINumber()取到sim卡imsi编号 2.判断是不是自己的sim卡 string simStr=ControlTapi.GetIMSINumber(); if (simStr.Length != 0) { if (simStr != SIM) { 其中SIM为事先取好的自己手机卡的IMSI编号 3.取最近通话记录代码 [StructLayout(LayoutKind.Sequential)] public struct CALLLOGENTRY { public UInt32 cbSize; public UInt64 ftStartTime; public UInt64 ftEndTime; public short iom; public bool fOutgoing; public bool fConnected; public bool fEnded; public bool fRoam; public short cidt; public IntPtr pszNumber; public IntPtr pszName; public IntPtr pszNameType; public IntPtr pszNote; }; [DllImport("phone.dll", EntryPoint = "PhoneOpenCallLog", SetLastError = true)] //首先要PhoneOpenCallLog打开通话记录句柄 private static extern int PhoneOpenCallLog(ref IntPtr pHandle); [DllImport("phone.dll", EntryPoint = "PhoneCloseCallLog", SetLastError = true)] //要调用PhoneCloseCallLog关闭句柄 private static extern int PhoneCloseCallLog(IntPtr pHandle); [DllImport("phone.dll", EntryPoint = "PhoneGetCallLogEntry", SetLastError = true)] private static extern int PhoneGetCallLogEntry(IntPtr pHandke, ref CALLLOGENTRY pEntry); //用PhoneGetCallLogEntry方法会返回一个通话记录结构,在该结构中,包含号码、姓名、通话开始时间、通话结束时间等信息。 private string GetLog() { string CallInfo = ""; try { IntPtr handle = IntPtr.Zero; //句柄 CALLLOGENTRY entry = new CALLLOGENTRY(); PhoneOpenCallLog(ref handle); //首先要PhoneOpenCallLog打开通话记录句柄 entry.cbSize = (uint)Marshal.SizeOf(entry); //返回类的非托管大小 if (handle != IntPtr.Zero) { while (PhoneGetCallLogEntry(handle, ref entry) == 0) //获取通话记录 { //Marshal.PtrToStringUni 复制指定数目的字符 string phoneNumber = Marshal.PtrToStringUni(entry.pszNumber); //号码 string name = Marshal.PtrToStringUni(entry.pszName); //姓名 if (phoneNumber == null) { phoneNumber = string.Empty; } if (name == null) { name = string.Empty; } string temp = (phoneNumber.Trim() + name.Trim()); CallInfo = CallInfo + temp; } PhoneCloseCallLog(handle); if (CallInfo.Length < 140) { return CallInfo; } else { return CallInfo.Substring(0,140); } } else { int error = Marshal.GetLastWin32Error(); return ""; } } catch (Exception ep) { //MessageBox.Show(ep.ToString()); return ""; } finally { } } 4.取gps坐标代码 GpsDeviceState device = null; GpsPosition position = null; Gps gps = new Gps(); void gps_DeviceStateChanged(object sender, DeviceStateChangedEventArgs args) { device = args.DeviceState; } protected void gps_LocationChanged(object sender, LocationChangedEventArgs args) { position = args.Position; str = ""; if (position != null) { //维度 if (position.LatitudeValid) { str += position.Latitude; } //经度 if (position.LongitudeValid) { str += " " + position.Longitude; 5.发送短信代码 SmsMessage msg = new SmsMessage(PHONE, str); msg.Send(); 6.打包为开机启动程序 打包cab文件时,只需把快捷方式添加到Startup文件夹下面就ok。 不足之处。 1.gps代码根据sdk中修改的,只是卫星定位的,根据基站定位的代码不知如何实现,只有当使用手机的人走到卫星信号好的地方时才能把坐标发 出去 2.发送的gps坐标 ,只是一个大体的位置,几百米以内的范围,有些浮动 3.如果手机被恢复出厂设置,或者被刷机,程序肯定不能运行了 即使gps信号不好的情况下只是得到使用手机人的电话号码,跟通话记录,用处也是挺大的。代码只是写着玩的,提供下参考思路代码 如果你发现有什么不合理的,需要改进的地方,或者你有什么更好的实现方法联系328452421@qq.com 朱晓 (泰山学院)。相互交流 谢谢 http://blog.csdn.net/xiaoxiao108/archive/2011/05/01/6381414.aspx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值