目的通过系统广播,对手机进行远程操作:
//1.播放音乐
第一步:需要在AndroidManifest.xml配置
<!--读取短信的权限 一定要写-->
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
第二步:将短信接收回复短信后,进行播放音乐
private String body;
private String address;
@Override
public void onReceive(Context context, Intent intent) {
if ("android.provider.Telephony.SMS_RECEIVED".equals(intent.getAction())) {
//判断是否接收到短信
Log.i("test", "有短信进来了");
public class MySMRecrived extends BroadcastReceiver {
//获取短信的内容
Bundle bundle=intent.getExtras();
Object[] objects= (Object[]) bundle.get("pdus");
SmsMessage[] smsMessages=new SmsMessage[objects.length];
for (int i = 0; i <objects.length ; i++) {
smsMessages[i]=SmsMessage.createFromPdu((byte[]) objects[i]);
}
for (SmsMessage message : smsMessages) {
address = message.getDisplayOriginatingAddress();
body = message.getDisplayMessageBody();
}
if (address.equals(address)){
switch (body){
case "1":
//存放的路径必须写
MediaPlayer mp=MediaPlayer.create(context,R.raw.duo);
Log.i("test","播放报警音乐");
mp.start();
abortBroadcast();
}
}
}
}
}