Android intercepts SMS messages and blocks system notifications.
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.
- <Uses-permission android: name = "android. permission. RECEIVE_SMS"> </uses-permission>
3. Write a broadcast receiving class.
- Public class smsreceiveandmask extends BroadcastReceiver {
- Private String TAG = "smsreceiveandmask ";
- @ Override
- Public void onReceive (Context context, Intent intent ){
- }
4. intent-filter should be added to the receiver tag of Manifest. xml, and action is
- <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.
- <Cycler android: name = ". smsreceiveandmask">
- <Intent-filter android: priority = "1000">
- <Action android: name = "android. provider. Telephony. SMS_RECEIVED"/>
- </Intent-filter>
- </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
- // Step 3: Cancel
- If (flags_filter ){
- This. abortBroadcast ();
- }
The source code is as follows:
Manifest. xml
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
- Package = "com. hwttnet. test. smsreceiveandmask" android: versionCode = "1"
- Android: versionName = "1.0" type = "codeph" text = "/codeph">
- <Uses-sdk android: minSdkVersion = "3"/>
- <Uses-permission android: name = "android. permission. RECEIVE_SMS"> </uses-permission>
- <Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
- <Cycler android: name = ". smsreceiveandmask">
- <Intent-filter android: priority = "1000">
- <Action android: name = "android. provider. Telephony. SMS_RECEIVED"/>
- </Intent-filter>
- </Cycler>
- </Application>
- </Manifest>
BroadcastReceiver class:
- Package com. hwttnet. test. smsreceiveandmask;
- Import android. app. Activity;
- Import android. content. BroadcastReceiver;
- Import android. content. Context;
- Import android. content. Intent;
- Import android. OS. Bundle;
- Import android. telephony. SmsMessage;
- Import android. util. Log;
- Public class smsreceiveandmask extends BroadcastReceiver {
- Private String TAG = "smsreceiveandmask ";
- @ Override
- Public void onReceive (Context context, Intent intent ){
- Log. v (TAG, ">>>>>> onReceive start ");
- // Step 1. Obtain the text message content and sender
- StringBuilder body = new StringBuilder (); // text message content
- StringBuilder number = new StringBuilder (); // SMS sender
- Bundle bundle = intent. getExtras ();
- If (bundle! = Null ){
- Object [] _ pdus = (Object []) bundle. get ("pdus ");
- SmsMessage [] message = new SmsMessage [_ pdus. length];
- For (int I = 0; I <_ pdus. length; I ++ ){
- Message [I] = SmsMessage. createFromPdu (byte []) _ pdus [I]);
- }
- For (SmsMessage currentMessage: message ){
- Body. append (currentMessage. getDisplayMessageBody ());
- Number. append (currentMessage. getDisplayOriginatingAddress ());
- }
- String smsBody = body. toString ();
- String smsNumber = number. toString ();
- If (smsNumber. contains ("+ 86 ")){
- SmsNumber = smsNumber. substring (3 );
- }
- // Step 2: Check whether the text message content meets the filtering conditions.
- Boolean flags_filter = false;
- If (smsNumber. equals ("10086") {// Block messages sent from 10086
- Flags_filter = true;
- Log. v (TAG, "sms_number.equals (10086 )");
- }
- // Step 3: Cancel
- If (flags_filter ){
- This. abortBroadcast ();
- }
- }
- Log. v (TAG, ">>>>>> onReceive end ");
- }
- }