This article describes the usage of BroadcastReceiver (broadcast receiver) among the four major components of Android programming. Share it for your reference, as follows:
Here we introduce how to create a broadcast, how to send an unordered broadcast and an orderly broadcast, as well as monitoring text messages and monitoring outgoing calls (when we send text messages and make calls, the system will send a broadcast, and we can intercept this broadcast to monitor text messages and monitor outgoing calls).
Define broadcast receivers
1. Define the class inherits BroadcastReceiver and overwrite the onReceive method
2. When the matched broadcast is received, the onReceive method will be executed.
3. Declare <receiver> in the manifest file, where <intent-filter> needs to be configured to specify the action and type of receiving broadcasts
In addition to declaring it in the manifest file, you can also declare it in the code and use the registerReceiver method to register Receiver.
Send a broadcast
Disordered broadcast
1. Use sendBroadcast method to send
2. Received by all broadcast receivers, disordered and cannot be interrupted
3. The recipient's permission can be set during broadcasting. Only if the recipient has permission can he receive it.
4. The receiver's <receiver> can also set the sender's permissions to only receive broadcasts containing permission applications.
Orderly broadcast
1. Use sendOrderedBroadcast method to send
2. The receiver can define the priority in the Android:priority in <intent-filter>. The larger the number, the higher the priority.
3. Received by each broadcast receiver one by one, and you can interrupt or add data in the middle.
abortBroadcast() //Interrupt broadcastgetResultExtras(true).putString("data", "New Data"); //Add datagetResultExtras(true).getString("data") //Receive data
Listen to SMS Receive
When the system receives the text message, it will send an orderly broadcast. If we define a recipient to receive the broadcast, we can get the text message content and intercept the text message.
2. Define the broadcast receiver to receive broadcast.SMS_RECEIVED
3. Call the getExtras() of the Intent inside the onReceive method to get the pdus field and get an Object[], where each element is a byte[]
4. Create SmsMessage object through the createFromPdu method of the SmsMessage class
5. You can obtain the sender number, SMS content, sending time and other information from the SmsMessage object.
6. Need to receive SMS permission:
The notification of receiving a text message in the system is an orderly notification. If we need to intercept spam messages, we can configure a higher priority and receive information to determine whether abortBroadcast()
Example:
List
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:andro package="" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="" /> <category android:name="" /> </intent-filter> </activity> <receiver android:name=".SmsReceiver"> <intent-filter android:priority="999"> <action android:name=".SMS_RECEIVED" /> </intent-filter> </receiver> </application> <uses-sdk android:minSdkVersion="8" /> <!-- Receive SMS permissions --> <uses-permission android:name=".RECEIVE_SMS"/> </manifest>
Listen to SMS broadcast:
package ; import ; import ; import ; import ; import ; import ; public class SmsReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Object[] pdus = (Object[]) ().get("pdus"); for (Object pdu : pdus) { //Create a text message SmsMessage sms = ((byte[]) pdu); //Get the sending mobile number String address = (); //Get the content of the text message String body = (); //Time to get text messages String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(())); (time); (address); (body); } //Interrupt the mobile phone reception operation abortBroadcast(); } }
Listen outgoing calls
1. Define broadcast receiver reception .NEW_OUTGOING_CALL
2. Permissions required
3. Use getResultData() and setResultData() methods in the onReceive method to get and set phone number
public void onReceive(Context context, Intent intent) { //Get the mobile phone number String num = getResultData(); // ... Query whether it is local //Operate the obtained phone number setResultData("17951" + num); }
life cycle
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 is finished.
2. Do not do some 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 child threads in broadcast receivers to do time-consuming work, because after the broadcast receiver is destroyed, the process becomes an empty process and is easily killed by the system.
4. Long-term work that takes time is best completed in service.
I hope this article will be helpful to everyone's Android programming design.