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
- <uses-permission android:name= "Android.permission.RECEIVE_SMS" ></uses-permission>
3. To write a broadcast receive class
- public class Smsreceiveandmask extends Broadcastreceiver {
- Private String TAG = "Smsreceiveandmask";
- @Override
- public void OnReceive (context context, Intent Intent) {
- }
4.manifest.xml the receiver tag to add intent-filter, action for
- <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
- <receiver android:name= ". Smsreceiveandmask" >
- <intent-filter android:priority= ">"
- <action android:name= "Android.provider.Telephony.SMS_RECEIVED"/>
- </intent-filter>
- </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
- Step Three: 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" >
- <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 android:name= ". Smsreceiveandmask" >
- <intent-filter android:priority= ">"
- <action android:name= "Android.provider.Telephony.SMS_RECEIVED"/>
- </intent-filter>
- </receiver>
- </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");
- The first step, get the content of the text message and the sender
- StringBuilder BODY = new StringBuilder ();//SMS 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 two: Confirm whether the content of this message satisfies the filter condition
- Boolean flags_filter = false;
- if (Smsnumber.equals ("10086")) {//shield 10086 sent SMS
- Flags_filter = true;
- LOG.V (TAG, "Sms_number.equals (10086)");
- }
- Step Three: Cancel
- if (flags_filter) {
- This.abortbroadcast ();
- }
- }
- LOG.V (TAG, ">>>>>>>onreceive end");
- }
- }