Android Intercept SMS and block the notification of the system

Source: Internet
Author: User

There are several key points to intercepting SMS:

1.android receive text messages in a broadcast manner

2. The program as long as in its own manifest.xml Riga has "receive" SMS Permissions

    1. <uses-permission android:name= "Android.permission.RECEIVE_SMS" ></uses-permission>

3. To write a broadcast receive class

    1. public class Smsreceiveandmask extends Broadcastreceiver {
    2. Private String TAG = "Smsreceiveandmask";
    3. @Override
    4. public void OnReceive (context context, Intent Intent) {
    5. }

4.manifest.xml the receiver tag to add intent-filter, action for

    1. <action android:name= "Android.provider.Telephony.SMS_RECEIVED"/>

5. It is important to add priority to this intent-filter to enable yourself to receive SMS over system or other software

    1. <receiver android:name= ". Smsreceiveandmask" >
    2. <intent-filter android:priority= ">"
    3. <action android:name= "Android.provider.Telephony.SMS_RECEIVED"/>
    4. </intent-filter>
    5. </receiver>

6. When your program receives SMS to be blocked, use This.abortbroadcast (), to end the broadcast continue to send to other programs, so that the system will not receive SMS broadcast, notification will not be prompted

    1. Step Three: Cancel
    2. if (flags_filter) {
    3. This.abortbroadcast ();
    4. }

The source code is as follows:

Manifest.xml

  1. <?xml version= "1.0" encoding= "Utf-8"?>
  2. <manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
  3. Package= "Com.hwttnet.test.smsreceiveandmask" android:versioncode= "1"
  4. Android:versionname= "1.0" >
  5. <USES-SDK android:minsdkversion= "3"/>
  6. <uses-permission android:name= "Android.permission.RECEIVE_SMS" ></uses-permission>
  7. <application android:icon= "@drawable/icon" android:label= "@string/app_name" >
  8. <receiver android:name= ". Smsreceiveandmask" >
  9. <intent-filter android:priority= ">"
  10. <action android:name= "Android.provider.Telephony.SMS_RECEIVED"/>
  11. </intent-filter>
  12. </receiver>
  13. </application>
  14. </manifest>

Broadcastreceiver class:

  1. Package com.hwttnet.test.smsreceiveandmask;
  2. Import android.app.Activity;
  3. Import Android.content.BroadcastReceiver;
  4. Import Android.content.Context;
  5. Import android.content.Intent;
  6. Import Android.os.Bundle;
  7. Import Android.telephony.SmsMessage;
  8. Import Android.util.Log;
  9. public class Smsreceiveandmask extends Broadcastreceiver {
  10. Private String TAG = "Smsreceiveandmask";
  11. @Override
  12. public void OnReceive (context context, Intent Intent) {
  13. LOG.V (TAG, ">>>>>>>onreceive start");
  14. The first step, get the content of the text message and the sender
  15. StringBuilder BODY = new StringBuilder ();//SMS Content
  16. StringBuilder number = new StringBuilder ();//SMS Sender
  17. Bundle bundle = Intent.getextras ();
  18. if (bundle! = null) {
  19. Object[] _pdus = (object[]) bundle.get ("PDUs");
  20. smsmessage[] message = new Smsmessage[_pdus.length];
  21. for (int i = 0; i < _pdus.length; i++) {
  22. Message[i] = SMSMESSAGE.CREATEFROMPDU ((byte[]) _pdus[i]);
  23. }
  24. for (Smsmessage currentmessage:message) {
  25. Body.append (Currentmessage.getdisplaymessagebody ());
  26. Number.append (Currentmessage.getdisplayoriginatingaddress ());
  27. }
  28. String smsbody = body.tostring ();
  29. String Smsnumber = number.tostring ();
  30. if (Smsnumber.contains ("+86")) {
  31. Smsnumber = smsnumber.substring (3);
  32. }
  33. Step two: Confirm whether the content of this message satisfies the filter condition
  34. Boolean flags_filter = false;
  35. if (Smsnumber.equals ("10086")) {//shield 10086 sent SMS
  36. Flags_filter = true;
  37. LOG.V (TAG, "Sms_number.equals (10086)");
  38. }
  39. Step Three: Cancel
  40. if (flags_filter) {
  41. This.abortbroadcast ();
  42. }
  43. }
  44. LOG.V (TAG, ">>>>>>>onreceive end");
  45. }
  46. }
Related Article

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.