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.
View plainprint?
- <Uses-Permission Android: Name = "android. Permission. receive_sms"> </uses-Permission>
<Uses-Permission Android: Name = "android. Permission. receive_sms"> </uses-Permission>
3. Write a broadcast receiving class.
View plainprint?
- Public class smsreceiveandmask extends broadcastreceiver {
- Private string tag = "smsreceiveandmask ";
- @ Override
- Public void onreceive (context, intent ){
- }
Public class smsreceiveandmask extends broadcastreceiver {private string tag = "smsreceiveandmask"; @ overridepublic void onreceive (context, intent ){}
4. Intent-filter should be added to the receiver tag of manifest. XML, and action is
View plainprint?
- <Action Android: Name = "android. provider. telephony. sms_received"/>
<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.
View plainprint?
- <Cycler Android: Name = ". smsreceiveandmask">
- <Intent-filter Android: Priority = "1000">
- <Action Android: Name = "android. provider. telephony. sms_received"/>
- </Intent-filter>
- </Cycler>
<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
View plainprint?
- // Step 3: Cancel
- If (flags_filter ){
- This. abortbroadcast ();
- }
// Step 3: cancel if (flags_filter) {This. abortbroadcast ();}
The source code is as follows:
Manifest. xml
View plainprint?
- <? 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>
<? 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 "> <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 "> <receiver er Android: Name = ". smsreceiveandmask "> <intent-filter Android: Priority =" 1000 "> <action Android: Name =" android. provider. telephony. sms_received "/> </intent-filter> </receiver> </Application> </manifest>
Broadcastreceiver class:
View plainprint?
- 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, 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 ");
- }
- }