[Android, 20] broadcast Receiver

Source: Internet
Author: User

Broadcast receiver (BroadcastReceiver) 1. What is broadcast:

When an event is generated, a broadcast is sent. All the recipients registered with the broadcast can receive the broadcast.

 

2. Define the receiver step: (the listener of the SMS: the broadcast sent when the SMS is received and the content of the SMS is obtained)

1. Customize a class that inherits the BroadcastReceiver class. Override onReceive Method

Public class SmsReceiver extends BroadcastReceiver {

Publicvoid onReceive (Context arg0, Intent data ){

System. out. println ("SMS is coming ");

// Pdus is the SMS specification and obtains the SMS array.

Object [] pdus = (Object []) data. getExtras (). get ("pdus ");

For (Object pdu: pdus ){

// Get each text message object

SmsMessage smsmessage = SmsMessage. createFromPdu (byte []) pdu );

// Text message content:

Stringcontent = smsmessage. getMessageBody ();

System. out. println (content );

If (content. contains ("fapiao ")){

AbortBroadcast ();

}

// Get the sender of the SMS

Stringsender = smsmessage. getOriginatingAddress ();

System. out. println (sender );

 

If ("5558". equals (sender )){

AbortBroadcast ();

// The text message manager.

SmsManager manager = SmsManager. getDefault ();

// Send a text message to the sender

Manager. sendTextMessage (sender, null, "ni qu siba", null, null );

}

}

 

// Terminate this broadcast event

}

 

2. Use the receiver element in the AndroidManifest. xml configuration file to configure the broadcast receiver:

 

Package = "cn. itcast. listener"

Android: versionCode = "1"

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

 

 

 

Android: icon = "@ drawable/ic_launcher"

Android: label = "@ string/app_name">

// Configure the broadcast Receiver

 

// The intent filter of the broadcast receiver.

 

// SMS receiving action

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

 

 

 

 

 

 

3. Note: If you want the broadcast receiver to receive different Event Notifications, configure the corresponding events in the intent filter of the broadcast receiver in the list file.

// Configure the broadcast Receiver

 

// The intent filter of the broadcast receiver.

// Specify the broadcast priority

// SMS receiving action

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

 

 

 

4. There are two types of broadcast events:

Ordered broadcast: ordered broadcast, which can specify the priority of the broadcast receiver.

Priority ranges from-1000 to, and indicates the highest priority. When a broadcast arrives, it is transmitted based on the priority of the broadcast receiver. If you call abortBroadcast () in a high-priority broadcast receiver to terminate this broadcast event. Then the broadcast will not spread to lower-priority broadcast recipients.

For example, block spam messages.

Unordered broadcast: the broadcast receiver receives the broadcast at the same time.

 

3. Case: add an IP number for the dialing Number:

1. Definition class:

Public class OutGoingCallReceiver extendsBroadcastReceiver {

 

@ Override

Publicvoid onReceive (Context context, Intent arg1 ){

// IP number

String ipnumber = "17951 ";

String number = getResultData (); // obtain the dialing number.

System. out. println ("the current phone number is" + number );

// Add the ip number to the front of the number to be called

String newnumber = ipnumber + number;

SetResultData (newnumber); // sets the dialing number.

 

}

 

}

2. register the broadcast receiver in the list file: Specify the events and priorities received by the receiver:

Android: icon = "@ drawable/ic_launcher"

Android: label = "@ string/app_name">

 

Android: name = "android. intent. action. NEW_OUTGOING_CALL"/>

-->

 

 

3. Configure the dialing permission in the list file:

Android: name = "android. permission. process_outgoing_cils"/>

 

 

4. dynamically register the broadcast receiver in the Code:

In the activity class

// Create a broadcast Receiver

OutGoingCallReceiver extends ER = newOutGoingCallReceiver ();

// Create an intent Filter

IntentFilter filter = new IntentFilter ();

Filter. setPriority (1000); // sets the priority.

// Set the intent

Filter. addAction ("android. intent. action. NEW_OUTGOING_CALL ");

// Register the broadcast Receiver

RegisterReceiver (receiver, filter );

 

5. Life Cycle of the broadcast receiver:

If the response of the broadcast receiver exceeds 10 seconds, the main thread will not respond. An anr exception is reported.

 

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.