手机防盗模块的实现

import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.text.TextUtils;

import com.study.mobilesafe.service.GPSService;

public class SmsReceiver extends BroadcastReceiver {

	private SharedPreferences sp;
	private DevicePolicyManager devicePolicyManager;
	private ComponentName who;

	// 接收到短信调用这个方法
	@Override
	public void onReceive(Context context, Intent intent) {
		sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
		Object[] objs = (Object[]) intent.getExtras().get("pdus");
		for (Object obj : objs) {
			SmsMessage message = SmsMessage.createFromPdu((byte[]) obj);
			String body = message.getMessageBody();

			if ("#*location*#".equals(body)) {
				abortBroadcast();// 截获手机短信
				// 打开服务 在服务中定位

				Intent gpsService = new Intent(context, GPSService.class);
				context.startActivity(gpsService);

				String latitude = sp.getString("latitude", "");
				String longitude = sp.getString("longitude", "");
				if (!TextUtils.isEmpty(longitude)
						&& !TextUtils.isEmpty(latitude)) {
					String safeNumber = sp.getString("safenum", "");
					SmsManager smsManager = SmsManager.getDefault();
					if (!TextUtils.isEmpty(safeNumber)) {
						smsManager.sendTextMessage(safeNumber, null, latitude
								+ "\n" + longitude, null, null);
					}
				} else if ("#*alarm*#".equals(body)) {
					abortBroadcast();

					// 调声音的最大值
					AudioManager audioManager = (AudioManager) context
							.getSystemService(Context.AUDIO_SERVICE);
					int maxVolume = audioManager
							.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
					audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
							maxVolume, 0);

					// 加载音乐资源
					MediaPlayer mediaPlayer = MediaPlayer.create(context,
							com.study.mobilesafe.R.raw.alarm);
					mediaPlayer.start();
				} else if ("#*wipedata*#".equals(body)) {
					abortBroadcast();
					who = new ComponentName(context,
							"com.study.mobilesafe.receiver.MyAdminReceiver");
					if (devicePolicyManager.isAdminActive(who)) {
						devicePolicyManager.wipeData(0);
					}
				} else if ("#*lock*#".equals(body)) {
					abortBroadcast();
					devicePolicyManager = (DevicePolicyManager) context
							.getSystemService(Context.DEVICE_POLICY_SERVICE);
					if (devicePolicyManager.isAdminActive(who)) {
						devicePolicyManager.lockNow();
					}
				}
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值