Android uses the broadcast receiver to listen for text messages and intercept outbound calls

Source: Internet
Author: User

If you want to listen to text messages received by yourself or others, and set the blacklist, you need the following functions to listen to text messages and handle prompts or responses:


When the system receives a text message, it sends a broadcast.Intent, Intent action name isAndroid. provider. telephony. sms_receivedThe intent stores the text message content received by the system. We use the name"PDUS"You can get the text message content from intent:

Public class incomingsmsreceiver extends broadcastreceiver {
Private Static final string sms_received = "android. provider. telephony. sms_received ";

@ Override

Public void onreceive (context, intent ){

If (intent. getaction (). Equals (sms_received )){
Smsmanager SMS = smsmanager. getdefault (); // get the SMS Manager
Bundle bundle = intent. getextras (); // obtain the bundle object in the intent
If (bundle! = NULL ){
Object [] PDUS = (object []) bundle. Get ("PDUS ");//Get the object named "PDUS", which is an object array. Each element in it is a byte [] array.
Smsmessage [] messages = new smsmessage [PDUS. Length];
For (INT I = 0; I <PDUS. length; I ++) messages [I] = smsmessage. createfrompdu (byte []) PDUS [I]);
For (smsmessage message: messages ){
String MSG = message. getmessagebody (); // text message content
String to = message. getoriginatingaddress (); // SMS address
SMS. sendtextmessage (to, null, MSG, null, null );

}

}

}

}

}

In the <Application> node of the androidmanifest. xml file, connect to the broadcast intent that receives the SMS for subscription:

<Cycler Android: Name = ". incomingsmsreceiver">

<Intent-filter>

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

</Intent-filter>

</Cycler>

Add the following permissions to the androidmanifest. xml file:
<Uses-Permission Android: Name = "android. Permission. receive_sms"/> <! -- Receive SMS -->
<Uses-Permission Android: Name = "android. Permission. send_sms"/> <! -- Send SMS permission -->

If you want to add a special prompt to the phone number or add the area code and 12593 discount number by default, you need to listen to the phone number for processing:

When a phone call is made, the system sends an ordered broadcast. Although the broadcast is eventually received by the broadcast receiver in the dial-up device, however, we can get the broadcast before the broadcast is passed to the broadcast receiver, and then clear the phone number sent to the broadcast receiver. When the broadcast receiver receives the broadcast, because the phone number is null, cancel the call.

Public class outgoingcallreceiver extends broadcastreceiver {
Public void onreceive (context, intent ){
Setresultdata (null); // clear the call. After the broadcast is sent to the receiver of the system, cancel the call because the call is null.

// Similarly, if you want to modify the phone number of the outbound caller, you can do this.
// String phone = getresultdata (); // obtain the outgoing call.
// Setresultdata ("12593" + phone); // Add 12593 in front of the phone
}
}

Receive an outbound call broadcast intent and subscribe to this intent in the <Application> node in the androidmanifest. xml file:
<Cycler Android: Name = ". outgoingcallreceiver">
<Intent-filter Android: Priority = "1">
<Action Android: Name = "android. Intent. Action. new_outgoing_call"/>
</Intent-filter>
</Cycler>
And you need to declare the permission:
<Uses-Permission Android: Name = "android. Permission. process_outgoing_cils"/>

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.