This section describes how to create a broadcast, how to send an unordered broadcast and ordered broadcast, and how to listen to text messages and outgoing calls. (when we send text messages and make phone calls, the system sends a broadcast, we can intercept this broadcast to listen for text messages and listen for outgoing calls ).
Define broadcast Receiver
1. Define the class to inherit BroadcastReceiver and override the onReceive method.
2. After receiving the matching broadcast, the onReceive method will be executed.
3. Declare <receiver ER> in the configuration file. You must configure <intent-filter> to specify the action and type for receiving broadcasts.
4. In addition to declaring BroadcastReceiver in the configuration file, you can also declare it in the Code and use the registerReceiver method to register the Receiver.
Send Broadcast
Unordered Broadcast
1. Send using sendBroadcast Method
2. received by all broadcast recipients, unordered, and not interrupted
3. the receiver permission can be set during broadcast. Only when the receiver has the permission can the receiver receive the message.
4. the receiver's <receiver> can also set the sender permission to only receive broadcasts containing the permission application.
Ordered Broadcast
1. Send using sendOrderedBroadcast Method
2. the receiver can define the priority for android: priority in <intent-filter>. A larger number indicates a higher priority.
3. Each broadcast receiver receives one by one. data can be interrupted or added in the middle.
AbortBroadcast () // interrupt Broadcast
GetResultExtras (true). putString ("data", "new data"); // Add data
GetResultExtras (true). getString ("data") // receives data
Listen to SMS reception
1. The Android system sends an ordered broadcast when receiving the text message. If we define a receiver to receive the broadcast, we can get the text message content or intercept the text message.
2. Define the broadcast receiver to receive broadcast android. provider. Telephony. SMS_RECEIVED
3. Call Intent's getExtras () in the onReceive method to obtain the pdus field and get an Object []. Each element is a byte [].
4. Create a SmsMessage object using the createFromPdu method of the SmsMessage class
5. You can obtain the sender's number, text message content, and sending time from the SmsMessage object.
6. You need to receive the SMS: <uses-permission android: name = "android. permission. RECEIVE_SMS"/>
7. notifications sent by SMS in the Android system are ordered. To intercept spam messages, you can configure a high priority and receive information to determine whether the SMS is abortBroadcast ()
Example:
List
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "cn. test"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
<Activity android: name = ". MainActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
<Cycler android: name = ". SmsReceiver">
<Intent-filter android: priority = "999">
<Action android: name = "android. provider. Telephony. SMS_RECEIVED"/>
</Intent-filter>
</Cycler>
</Application>
<Uses-sdk android: minSdkVersion = "8"/>
<! -- Receive SMS -->
<Uses-permission android: name = "android. permission. RECEIVE_SMS"/>
</Manifest>
Listen to SMS broadcast:
Package cn. test;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. telephony. SmsMessage;
Public class SmsReceiver extends BroadcastReceiver {
Public void onReceive (Context context, Intent intent ){
Object [] pdus = (Object []) intent. getExtras (). get ("pdus ");
For (Object pdu: pdus ){
// Create an SMS
SmsMessage sms = SmsMessage. createFromPdu (byte []) pdu );
// Obtain the sent mobile phone number
String address = sms. getOriginatingAddress ();
// Obtain the text message content
String body = sms. getMessageBody ();
// Get the SMS time
String time = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"). format (new Date (sms. getTimestampMillis ()));
System. out. println (time );
System. out. println (address );
System. out. println (body );
}
// Interrupt the mobile phone reception operation
AbortBroadcast ();
}
}
Listen for outgoing calls
1. Define the broadcast receiver to receive android. intent. action. NEW_OUTGOING_CALL
2. permission required <uses-permission android: name = "android. permission. process_outgoing_cils"/>
3. Use the getResultData () and setResultData () methods in the onReceive method to obtain and set the phone number.
View plain
Public void onReceive (Context context, Intent intent ){
// Obtain the mobile phone number
String num = getResultData ();
//... Check whether it is local
// Perform operations on the obtained phone number
SetResultData ("17951" + num );
}
Lifecycle
1. the life cycle of the broadcast receiver is very short. It is created when the broadcast is received and destroyed after the onReceive () method ends.
2. Do not perform time-consuming work in the broadcast receiver; otherwise, the Application No Response error dialog box will pop up.
3. It is best not to create a sub-thread in the broadcast receiver for time-consuming work, because after the broadcast receiver is destroyed, the process becomes an empty process and is easily killed by the system.
4. It is best to put the time-consuming work in the service for completion.
From: Fu rongkang Column