Android intercepts SMS messages and blocks system notifications

Source: Internet
Author: User

There are several key points to intercept text messages:

 

1. android receives text messages in broadcast mode.

2. You only need to add the "receive" SMS permission to your Manifest. xml file.

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

 

3. Write a broadcast receiving class.

  1. Public class smsreceiveandmask extends BroadcastReceiver {
  2. Private String TAG = "smsreceiveandmask ";
  3. @ Override
  4. Public void onReceive (Context context, Intent intent ){
  5. }

 

4. intent-filter should be added to the receiver tag of Manifest. xml, and action is

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

 

5. It is important to add the priority on the intent-filter so that you can receive SMS messages that take precedence over the system or other software.

  1. <Cycler android: name = ". smsreceiveandmask">
  2. <Intent-filter android: priority = "1000">
  3. <Action android: name = "android. provider. Telephony. SMS_RECEIVED"/>
  4. </Intent-filter>
  5. </Cycler>

 

6. when your program receives the SMS to be blocked, use this. abortBroadcast (); to end broadcasting and send it to other programs, so that the system will not receive text message broadcast and Notification will not prompt

  1. // Step 3: 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" type = "codeph" text = "/codeph">
  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. <Cycler android: name = ". smsreceiveandmask">
  9. <Intent-filter android: priority = "1000">
  10. <Action android: name = "android. provider. Telephony. SMS_RECEIVED"/>
  11. </Intent-filter>
  12. </Cycler>
  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. // Step 1. Obtain the text message content and sender
  15. StringBuilder body = new StringBuilder (); // text message 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 2: Check whether the text message content meets the filtering conditions.
  34. Boolean flags_filter = false;
  35. If (smsNumber. equals ("10086") {// Block messages sent from 10086
  36. Flags_filter = true;
  37. Log. v (TAG, "sms_number.equals (10086 )");
  38. }
  39. // Step 3: 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.