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();
}
}
}
}
}
}
手机防盗模块的实现
最新推荐文章于 2020-05-31 23:01:20 发布