Detailed Explanation of BroadcastReceiver of Four Components of Android

  • 2021-11-01 04:36:56
  • OfStack

BroadcastReceiver (Broadcast Receiver), in the development of Android, BroadcastReceiver has many application scenarios, which belongs to one of the major components of Android4.

Android Broadcast has two roles: Broadcast Sender and Broadcast Receiver

1. Role

Used to listen/receive broadcast messages sent by applications and respond to them

Application scenario:

Communication between different components (including within applications/between different applications) Communication with the Android system in specific situations (such as when the network is available when the phone calls in) Multithreaded communication

2. Implementation principle

Broadcast in Android uses the observer pattern of design pattern: the message-based publish/subscribe event model.

There are three roles in the model:

Message Subscriber (Broadcast Recipient) Message publisher (broadcast publisher) Message Center (AMS, i.e. Activity Manager Service)

Broadcast recipients register with AMS through Binder mechanism
The broadcast sender sends the broadcast to the AMS through the Binder mechanism
AMS searches for suitable broadcast recipients in the registered list according to the requirements of broadcast senders (search basis: IntentFilter/Permission)
AMS sends the broadcast to the corresponding message circular queue of the appropriate broadcast recipient;
The broadcast receiver gets this broadcast through a message loop and calls back onReceive ()

3. Broadcast Receiver Registration

There are two ways to register: static registration and dynamic registration

Static registration

Declare through tags in AndroidManifest. xml


<receiver
 // This broadcast receiver class is mBroadcastReceiver
 android:name=".mBroadcastReceiver" >
 // Used to receive broadcasts when the network state changes 
 <intent-filter>
   <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
 </intent-filter>
</receiver>

When this App is first started, the mBroadcastReceiver class is automatically instantiated and registered in the system.

Static registration is resident broadcast and is not affected by any component life cycle

Dynamic registration

Register BroadcastReceiver dynamically in code by calling the * registerReceiver () method of Context


@Override
protected void onResume() {
  super.onResume();
  // Instantiation BroadcastReceiver Subclass  & IntentFilter
  mBroadcastReceiver mBroadcastReceiver = new mBroadcastReceiver();
  IntentFilter intentFilter = new IntentFilter();
  // Set the type of broadcast received 
  intentFilter.addAction(android.net.conn.CONNECTIVITY_CHANGE);
  // Call Context Adj. registerReceiver () method for dynamic registration 
  registerReceiver(mBroadcastReceiver, intentFilter);
}

After registering the broadcast, remember to destroy the broadcast at the corresponding location

That is, unregisterReceiver (mBroadcastReceiver) in onPause ()

When this Activity is instantiated, the MyBroadcastReceiver is 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 in onResume() Broadcast in the method 
  unregisterReceiver(mBroadcastReceiver);
}

Note:

Dynamic broadcast is preferably registered at onResume () of Activity and logged off at onPause ().

Reason:

For dynamic broadcast, if there is registration, there must be logout, otherwise it will lead to memory leakage
Repeated registration and cancellation are not allowed
Dynamic registration is non-resident broadcast, flexible, and follows the life cycle changes of components.

Summarize


Related articles: