14. Intercept SMS

Source: Internet
Author: User

Intercept SMS, Play music (app system sounds, also this logic)
  
 
  1. /**
  2. * 拦截短信
  3. *
  4. * @author Kevin
  5. *
  6. */
  7. public class SmsReceiver extends BroadcastReceiver {
  8. @Override
  9. Span class= "KWD" >public void onreceive ( context Context intent Intent ) {
  10. Object[] objects = (Object[]) intent.getExtras().get("pdus");
  11. for (Object object : objects) {// 短信最多140字节,
  12. // 超出的话,会分为多条短信发送,所以是一个数组,因为我们的短信指令很短,所以for循环只执行一次
  13. SmsMessage message = SmsMessage.createFromPdu((byte[]) object);
  14. String originatingAddress = message.getOriginatingAddress();// 短信来源号码
  15. String messageBody = message.getMessageBody();// 短信内容
  16. System.out.println(originatingAddress + ":" + messageBody);
  17. if ("#*alarm*#".equals(messageBody)) {
  18. // 播放报警音乐, 即使手机调为静音,也能播放音乐, 因为使用的是媒体声音的通道,和铃声无关。在res目录下新建raw目录用来存放声音文件
  19. MediaPlayer player = MediaPlayer.create(context, R.raw.ylzs);
  20. player.setVolume(1f, 1f);//左右声道
  21. player.setLooping(true);//是否循环
  22. player.start();
  23. abortBroadcast();// 中断短信的传递, 从而系统短信app就收不到内容了
  24. } else if ("#*location*#".equals(messageBody)) {
  25. // 获取经纬度坐标
  26. context.startService(new Intent(context, LocationService.class));// 开启定位服务
  27. SharedPreferences sp = context.getSharedPreferences("config",
  28. Context.MODE_PRIVATE);
  29. String location = sp.getString("location",
  30. "getting location...");
  31. System.out.println("location:" + location);
  32. abortBroadcast();// 中断短信的传递, 从而系统短信app就收不到内容了
  33. } else if ("#*wipedata*#".equals(messageBody)) {
  34. System.out.println("远程清除数据");
  35. abortBroadcast();
  36. } else if ("#*lockscreen*#".equals(messageBody)) {
  37. System.out.println("远程锁屏");
  38. abortBroadcast();
  39. }
  40. }
  41. }
  
 
  1. //可以写int的最大值2147483647
  2. <receiver android:name=".receiver.SmsReceiver" >
  3. <intent-filter android:priority="2147483647" >
  4. <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  5. </intent-filter>
  6. </receiver>








From for notes (Wiz)

14. Intercept SMS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.