Describes how to create a broadcast, send an out-of-order broadcast and orderly broadcast, listen to text messages, and listen to outgoing calls (when we text and make a call, the system sends a broadcast that we can intercept to listen for text messages and listen for outgoing calls).
Define broadcast receivers
1. Defining class inheritance Broadcastreceiver, overriding the OnReceive method
2. The OnReceive method is executed when a matching broadcast is received
3. Declare <receiver> in the manifest file, where you need to configure <intent-filter> specify the actions and types of the receive broadcast
4.BroadcastReceiver, in addition to being declared in the manifest file, can also be declared in the code, using the Registerreceiver method to register receiver
Send broadcast
Shuffle broadcast
1. Use the Sendbroadcast method to send
2. Received by all broadcast receivers, unordered, non-disruptive
3. You can set recipient permissions when broadcasting, only if the recipient has permission to receive
4. Recipient's <receiver> can also set sender permissions, only receive broadcast with permission app
Orderly broadcast
1. Use the Sendorderedbroadcast method to send
2. Recipients can define android:priority in <intent-filter> to define priority, the higher the number the higher the priority
3. Received by each broadcast recipient, can interrupt or add data halfway
Abortbroadcast ()//Interrupt Broadcast
Getresultextras (True). Putstring ("Data", "new"); Add data
Getresultextras (True). GetString ("Data")//Receive data
Listen to SMS Receive
The 1.Android system sends an ordered broadcast when a message is received, and if we define a recipient to receive the broadcast, we can get the text message and intercept the SMS.
2. Define broadcast receivers to receive broadcast Android.provider.Telephony.SMS_RECEIVED
3. Call Intent's Getextras () inside the OnReceive method to get the PDUs field, and get a object[], where each element is a byte[]
4. Creating Smsmessage objects through the Createfrompdu method of the Smsmessage class
5. From the Smsmessage object, you can get the sender number, text message content, sending time and other information
6. Need to receive SMS permission: <uses-permission android:name= "Android.permission.RECEIVE_SMS"/>
7.Android system in the receipt of SMS notification is an ordered notification, we need to intercept spam messages, you can configure a higher priority, receive information to determine whether Abortbroadcast ()
Example:
Listing
- <? 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">
- < 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>
- <receiver android:name=". Smsreceiver ">
- <intent-filter android:priority = "999" >
- <action android:name="Android.provider.Telephony.SMS_RECEIVED" />
- </intent-filter>
- </receiver>
- </Application>
- <uses-sdk android:minsdkversion="8" />
- <!--receive SMS rights --
- <uses-permission android:name="Android.permission.RECEIVE_SMS"/>
- </manifest>
Listen to SMS broadcasts:
- 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 a text message
- Smsmessage SMS = SMSMESSAGE.CREATEFROMPDU ((byte[]) PDU);
- //Get the phone number to send
- String address = sms.getoriginatingaddress ();
- //Get the content of the SMS
- String BODY = Sms.getmessagebody ();
- //Time to get SMS
- 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 phone receive Operation
- Abortbroadcast ();
- }
- }
Listen to the outgoing call
1. Define the broadcast recipient to receive Android.intent.action.NEW_OUTGOING_CALL
2. Requires permission <uses-permission android:name= "Android.permission.PROCESS_OUTGOING_CALLS"/>
3. Use the Getresultdata () and Setresultdata () methods in the OnReceive method to get and set the phone number
- Public void onreceive (context context, Intent Intent) {
- //Get to phone number
- String num = Getresultdata ();
- // ... Whether the query is local
- //operation of the acquired phone number
- Setresultdata ("17951" + num);
- }
Life cycle
1. The life cycle of the broadcast receiver is very short, created when the broadcast is received, and destroyed after the OnReceive () method finishes
2. Do not take some time-consuming work in the broadcast receiver, or the Application No Response error dialog box will pop up
3. It is best not to create sub-threads in the broadcast receiver to do time-consuming work, because the broadcast receiver is destroyed after the process becomes an empty process, it is easy to be killed by the system
4. Time-consuming longer work is best done in the service
Broadcast recipient of Android four components