Local Broadcast Manager in the Android Support library (Principle Analysis of Local Broadcast Manager in Android Support Library)

Local Broadcast Manager principle analysis in the Android Support library Introduction: Local Broadcast Manager is a local radio manager provided in the Android Support library to send and receive broadcasts internally.Compared with global broadcasting, local broadcasting is more efficient and secure, and only these broadcasts can be received only internal components.This article will in -depth analysis of the principles of Local Broadcast Manager and how to use it to send and receive local broadcasts. Analysis of Local Broadcast Manager principle: The core of Local Broadcast Manager is a singles class. By allowing component registration and receiving broadcast in the application, the purpose of communicating internally.It uses Handler and ArrayList inside the application to achieve broadcasting and receiving. 1. Register a broadcast receiver: In the application component (such as Activity, Service, etc.), you can register a broadcast receiver by calling LocalBroidCastManager () methods of the registerReceiver () method of LocalBroadCastManager.When registering, you need to pass a BroadCastReceiver object and an IntentFilter object to specify the type of broadcast type.After the registration is successful, the component can receive a local broadcast that matches the IntentFilter. The example code is as follows: private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // Treat the receiving broadcast } }; private IntentFilter mFilter = new IntentFilter("com.example.ACTION_NAME"); @Override protected void onResume() { super.onResume(); LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, mFilter); } @Override protected void onPause() { super.onPause(); LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver); } 2. Send local broadcast: It is very simple to use Local Broadcast Manager to send local broadcasts. Just call the SendBroadcast () method and pass it into an Intent object.Send a broadcast by specifying the ACTION that specifies the INTENT, the receiver can match the receiving the corresponding type of broadcast through the IntentFilter. The example code is as follows: Intent intent = new Intent("com.example.ACTION_NAME"); LocalBroadcastManager.getInstance(this).sendBroadcast(intent); Local Broadcast Manager advantage: -Hirgitivity: Local Broadcast Manager only transmits broadcasts within the application and does not need to pass the system broadcast mechanism, so it is faster and reduces the use of system resources. -Apay: Since the local broadcast is only transmitted inside the application, other applications cannot receive these broadcasts, which improves the security of the application. -Dimly granularity control: Using local broadcasts can control the receiving components very flexibly, and only send the broadcast to the required components. in conclusion: Local Broadcast Manager is a powerful local broadcast manager provided in the Android Support library.It allows efficient, secure, and fine -grained communications between the internal components.By understanding the principles and usage methods of Local Broadcast Manager, you can make full use of this feature to meet the application's communication needs.