Android programming to block SMS and block Notification system method

  • 2020-11-18 06:27:10
  • OfStack

This paper illustrates the Android programming method to block SMS and block Notification. To share for your reference, the details are as follows:

There are a few key points about SMS interception:

1.android receives SMS messages via radio

2. The program only needs to add "receive "SMS to its own ES10en.xml


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

3. Write a broadcast receiver class


public class smsreceiveandmask extends BroadcastReceiver {
  private String TAG = "smsreceiveandmask";
  @Override
  public void onReceive(Context context, Intent intent) {
}
public class smsreceiveandmask extends BroadcastReceiver {
  private String TAG = "smsreceiveandmask";
  @Overridepublic void onReceive(Context context, Intent intent) {} 

4. Add ES22en-ES23en and action to the receiver tag of Manifest


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

5. It is important to put the priority priority on the ES28en-ES29en so that you receive SMS over the system or other software


<receiver android:name=".smsreceiveandmask" > 
      <intent-filter android:priority="1000">  
        <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
      </intent-filter> 
    </receiver> 
<receiver android:name=".smsreceiveandmask" >
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

6. When your own program receives SMS to be blocked, use this. abortBroadcast(); To end the broadcast and send it to another program, so that the system will not receive the SMS broadcast, and Notification will not be prompted


//  The first 3 step : cancel  
if (flags_filter) {
  this.abortBroadcast();
}
//  The first 3 step : cancel if (flags_filter) {this.abortBroadcast();}

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="1000">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
      </intent-filter>
    </receiver>
  </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 android:name=".smsreceiveandmask" >
<intent-filter android:priority="1000">
<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 1 Step, get the content of the message and the sender 
    StringBuilder body = new StringBuilder();//  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);
      }
      //  The first 2 step : Verify that the message content meets the filtering criteria 
      boolean flags_filter = false;
      if (smsNumber.equals("10086")) {//  shielding 10086 Text message from 
        flags_filter = true;
        Log.v(TAG, "sms_number.equals(10086)");
      }
      //  The first 3 step : cancel 
      if (flags_filter) {
        this.abortBroadcast();
      }
    }
    Log.v(TAG, ">>>>>>>onReceive end");
  }
}

I hope this article has been helpful for Android programming.


Related articles: