Implementation of SMS interception on Android phones

Source: Internet
Author: User

I was very interested in functions such as text message interception when I first started to use Android. Many articles on the Internet have introduced the use of broadcast to receive android. provider. telephony. sms_received action to receive the SMS message, but I didn't know how to delete the broadcast at the time, so as to implement a function similar to the SMS blacklist. Later, I saw on the Internet that I could use abortbroadcast to block broadcasting. I tested it and it was feasible.

First go to the source code:

Androidmanifest. XML code:

<? XML version = "1.0" encoding = "UTF-8"?>

<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"

Package = "com. Study. SMS"

Android: versioncode = "1"

Android: versionname = "1.0" type = "codeph" text = "/codeph">

<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">

<Cycler Android: Name = ". smsreceiver">

<Intent-filter Android: Priority = "1000">

<Action Android: Name = "android. provider. telephony. sms_received"/>

</Intent-filter>

</Cycler>

</Application>

<Uses-Permission Android: Name = "android. Permission. receive_sms">

</Uses-Permission>

</Manifest>

Code for smsreceiver:

Package com. Study. SMS;

 

Import Android. content. broadcastreceiver;

Import Android. content. context;

Import Android. content. intent;

Import Android. OS. Bundle;

Import Android. telephony. smsmessage;

 

Public class smsreceiver extends broadcastreceiver {

Public static final string sms_received_action = "android. provider. telephony. sms_received ";

@ Override

Public void onreceive (context, intent ){

String action = intent. getaction ();

If (sms_received_action.equals (Action )){

Bundle bundle = intent. getextras ();

If (bundle! = NULL ){

Object [] PDUS = (object []) bundle. Get ("PDUS ");

For (Object PDU: PDUS ){

Smsmessage message = smsmessage. createfrompdu (byte []) PDU );

String sender = message. getoriginatingaddress ();

If ("5556". Equals (sender )){

// Block the text message with the mobile phone number 5556. You can handle the text message, such as sending the message to a third-party mobile phone.

Abortbroadcast ();

}

System. Out. println (message. getoriginatingaddress () + "," + message. getmessagebody () + "," + message. getindexonicc ());

}

}

}

}
}

Tested:

 

 

If you use 5556 to send a text message to 5554, 5554 cannot receive it. At the same time, it is normal to initiate another 5558 AVD to send text messages to 5554.

This program can block system text messages because broadcast has two different types: normal broadcasts and ordered broadcasts ). Normal broadcast is completely asynchronous and can be received by all receivers, and the receiver cannot terminate the broadcast. However, ordered broadcast is received by the receiver in sequence based on the priority level stated by the receiver. Priority Level Declaration is in the Android: Priority attribute of intent-filter element. The higher the number, the higher the priority level. value range:-1000 to 1000. You can also call setpriority () of the intentfilter object at the priority level (). The receiver of an ordered broadcast can terminate the spread of the broadcast intent. Once the spread of the broadcast intent is terminated, the subsequent receiver cannot receive the broadcast. The above example sets the priority of smsreceiver to 1000.

 

Secondly, we need to implement the onreceive method. Based on the above code, we can get the text message content from the bundle and restore it to smsmessage, in this way, we can determine what needs to be intercepted based on specific conditions, and what can be checked. For text messages that need to be intercepted, we can directly intercept abortbroadcast () After receiving the message (the code above judges that the text message is intercepted when the content contains hahaha ), in this way, the system inbox will not receive this text message, and no notification will be sent to the user.

At this point, after the application is compiled, the text message interception function is complete. Of course, you can also do some other functions based on this, which depends on your needs. Note: If the priorities are the same, broadcast is transmitted Based on the package name.

To delete text messages that already exist in the inbox, refer to the smsprovider class of Android source code. You can use the contentprovider mechanism in your application for operations.

 

Context. sendbroadcast ()

A normal broadcast is sent, and all subscribers have the opportunity to obtain and process it.

Context. sendorderedbroadcast ()

An ordered broadcast is sent, and the system will execute the receiver one by one based on the priority level stated by the receiver.

 

I checked that the system sent android. provider. telephony. sms_received broadcast source code smsdispatcher. java (frameworks/base/telephony/Java/COM/Android/Internal/telephony) confirms that it sends an ordered broadcast.

Protected void dispatchpdus (byte [] [] PDUS ){

Intent intent = new intent (intents. sms_received_action );

Intent. putextra ("PDUS", PDUS );

Dispatch (intent, "android. Permission. receive_sms ");

}

Public void dispatch (intent, string permission ){
// Hold a wake lock for wake_lock_timeout seconds, enough to give any
// Receivers time to take their own wake locks.
Mwakelock. Acquire (wake_lock_timeout );
Mcontext. sendorderedbroadcast (intent, permission, mresultreceiver,
This, activity. result_ OK, null, null );
}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/prince58/archive/2011/03/10/6237792.aspx

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.