BroadcastReceiver
1, Definition
-
BroadcastReceiver is one of the four components of Android
android broadcast is divided into two roles: Broadcast sender and broadcast receiver
2, Function
-
It is used to monitor / receive the broadcast information sent by the application and respond
-
Application scenario
a. Communication between different components (in application and different applications)
b. Communicate with Android system components
c. Multithreaded communication
3, Implementation principle
-
The broadcast in Android uses the observer mode in the design mode: message based subscription and publishing event model.
-
Three roles in the model:
1. Message subscriber (broadcast receiver)
2. Message publisher (broadcast sender)
3. activity manager service
[the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-lagkuruw-1622624470159) (/ users / Gewu / library / Application Support / typera user images / screenshot 2021-06-02 12.16.20.png)]
-
Principle description:
The broadcast receiver registers in AMS through Binder mechanism
Broadcast sender sends broadcast to AMS through Binder mechanism
AMS searches for a suitable broadcast receiver in the registered list according to the requirements of the broadcast sender
AMS sends the broadcast to the corresponding message loop queue of the appropriate broadcast receiver
The broadcast receiver gets the broadcast through the message loop and calls back onReceive()
Special note: the execution of the broadcast sender and the broadcast receiver is asynchronous, and the broadcast sent will not care about the problem
There is no receiver and it is uncertain when the receiver will receive it
4, Specific use
4.1 user defined broadcast receiver
-
Inherit BroadcastReceivre
-
Abstract method onReceive() must be replicated
When the corresponding receiver of the broadcast is called, recon1. Will automatically receive the broadcast
2. Generally, onRecevice involves the interaction between components, such as sending notification and starting service
3. By default, it runs in the UI thread, so onrevise cannot perform time-consuming operations
-
Code demonstration
public class mBroadcastReceiver extends BroadcastReceiver { //This method is called automatically after receiving the broadcast @Override public void onReceive(Context context, Intent intent) { //Write operation after receiving broadcast } }
4.2 broadcast receiver registration
There are two ways to register: one is static registration and the other is dynamic registration.
4.2.1 static registration
-
Label declaration in Android manifest
-
Attribute description
<receiver android:enabled=["true" | "false"] //Can this broadcastReceiver receive broadcasts from other apps //The default value is determined by whether there is an intent filter in the receiver: if there is an intent filter, the default value is true, otherwise it is false android:exported=["true" | "false"] android:icon="drawable resource" android:label="string resource" //Class name inheriting the subclass of BroadcastReceiver android:name=".mBroadcastReceiver" //Only the broadcast sent by the broadcast sender with corresponding permission can be received by this BroadcastReceiver; android:permission="string" //The process in which BroadcastReceiver runs //The default is app process. You can specify an independent process //Note: the four basic components of Android can specify their own independent processes through this attribute android:process="string" > //Specifies the type of broadcast this broadcast receiver will receive //In this example, it is used to receive the broadcast when the network state changes <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> </intent-filter> </receiver>
-
Registration example
<receiver //This broadcast receiver class is mBroadcastReceiver android:name=".mBroadcastReceiver" > //Used to receive the broadcast when the network state changes <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver>
When this App is started for the first time, the system will automatically instantiate the mbaroadcastreceiver class and register it with the system
Tongzhong.
4.2.2 dynamic registration
In the code, dynamically register BroadcastReceiver by calling the registerReceiver() method of Context. The specific code is as follows:
@Override protected void onResume() { super.onResume(); //Instantiate the subclass of BroadcastReceiver & intentfilter mBroadcastReceiver mBroadcastReceiver = new mBroadcastReceiver(); IntentFilter intentFilter = new IntentFilter(); //Set the type of broadcast received intentFilter.addAction(android.net.conn.CONNECTIVITY_CHANGE); //Call the registerReceiver() method of Context for dynamic registration registerReceiver(mBroadcastReceiver, intentFilter); } //After registering the broadcast, remember to destroy the broadcast in the corresponding position //That is, unregisterReceiver(mBroadcastReceiver) in onPause() //When this Activity is instantiated, MyBroadcastReceiver will be dynamically registered in the system //When this Activity is destroyed, the dynamically registered MyBroadcastReceiver will no longer receive the corresponding broadcast. @Override protected void onPause() { super.onPause(); //Destroy the broadcast in the onResume() method unregisterReceiver(mBroadcastReceiver); }
-
Particular attention
Dynamic broadcasting is best registered in onResume() and cancelled in onPause() of Activity.
Reason: for dynamic broadcasting, if there is registration, there must be logout, otherwise it will lead to memory leakage
4.2.3 differences between two registrations
4.3 broadcast sender sends broadcast to AMS
4.3.1 transmission of broadcast
- Broadcasting is identified by "intention"
- Define the essence of Broadcasting: define the "intention" of broadcasting
- Broadcast sending: the broadcast sender sends the "intention" of this broadcast through * * sendBroadcast() * * method
4.3.2 type of broadcast
-
Normal broadcast
That is, the developer's own definition intent Broadcast of(Most commonly used). Send broadcast using the following: Intent intent = new Intent(); //action corresponding to intentFilter in BroadcastReceiver intent.setAction(BROADCAST_ACTION); //Send broadcast sendBroadcast(intent); //If the action of intentFilter matches the above when registering in the registered broadcast receiver, the broadcast will be received (i.e. callback onReceive()). //The following mBroadcastReceiver will receive the above broadcast <receiver //This broadcast receiver class is mBroadcastReceiver android:name=".mBroadcastReceiver" > //Used to receive the broadcast when the network state changes <intent-filter> <action android:name="BROADCAST_ACTION" /> </intent-filter> </receiver> If the sending broadcast has corresponding permission, the broadcast receiver also needs corresponding permission
-
System broadcast
-
Android has built-in multiple system broadcasts: as long as it involves the basic operation of the mobile phone (such as power on, network state change, taking photos, etc.), it will send out corresponding broadcasts
-
Each broadcast has a specific intent - filter (including specific actions). The broadcast actions of Android system are as follows:
-
-
Ordered broadcast
-
Defines that the broadcast sent is received by the broadcast receiver in sequence
-
Sequence rules for broadcast recipients to receive broadcasts (for both static and dynamic registered broadcast recipients)
-
characteristic
-
Receive broadcasts in sequence
-
The receiver of the first received broadcast can cut off the broadcast, that is, the receiver of the later received broadcast will no longer receive the broadcast
Receive this broadcast;
-
The first received broadcast receiver can modify the broadcast, and the later received broadcast receiver will receive the broadcast
Receive modified broadcast
-
-
-
Sticky broadcast
Because in Android 5 0 & API 21 has been invalidated, so it is not recommended to use it, and there is no excessive summary here
Knot.
-
App local broadcast
-
Background the broadcast in Android can communicate directly across apps (exported for the case with intent filter)
(the default value is true)
-