The method of intercepting SMS and shielding system notification by Android programming _android

Source: Internet
Author: User

This article describes the Android program to intercept SMS and shielding system notification method. Share to everyone for your reference, specific as follows:

There are several key points to intercept SMS:

1.android receives the message by the way of broadcasting

2. The program as long as in their own manifest.xml Riga have "receive" SMS Permissions

<uses-permission android:name= "Android.permission.RECEIVE_SMS" >
</uses-permission>
< 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) {
} public
class Smsreceiveandmask extends Broadcastreceiver {
  private String TAG = "Smsreceiveandmask";
  @Overridepublic void OnReceive (context context, Intent Intent) {} 

4.manifest.xml's receiver tag to add intent-filter, action for

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

5. It is important to add priority priority to this intent-filter so that you receive SMS prior to 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 program receives the SMS to be screened, use This.abortbroadcast () to end the broadcast to continue to send other programs, so that the system will not receive SMS broadcast, notification will not be prompted

Step Three: Cancel 
if (flags_filter) {
  this.abortbroadcast ();
}
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=" 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:prio rity= "1000" > <action android:name= "Android.provider.Telephony.SMS_RECEIVED"/> </intent-filter&
    Gt </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 step, get the content of the text message and 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 SMS content 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");

 }
}

I hope this article will help you with your Android programming.

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.