Detailed explanation of the Local Broadcast Manager framework in the Android SUPPORT Library (Detailed Explanation of the Framework Principles of Local Broadcast Manager in Android SUPPPPORARY)

The Local Broadcast Manager framework in the Android SUPPORT library is a powerful tool for making messages inside the application.It allows to communicate between components without having to go through the system.This article will explain the working principles of Local Broadcast Manager and provide some Java code examples. Local Broadcast MANAGER uses the Publish-SubScrip design mode to implement communication between components.It consists of two main components: sender and receiver.The sender's component sent a message and sent it to Local Broadcast Manager, and the receiver component registered himself to receive a specific type of message. To use the Local Broadcast Manager, you first need to add the dependency item of the SUPPORT library to the application.Add the following dependencies to the Build.gradle file: gradle implementation 'com.android.support:support-v4:your_version' Next, we will create examples of a sender component and a receiver component.First, we will create a class called MESSAGESENDER, which is responsible for sending messages.code show as below: import android.content.Context; import android.content.Intent; import android.support.v4.content.LocalBroadcastManager; public class MessageSender { private Context mContext; public MessageSender(Context context) { mContext = context; } public void sendMessage(String message) { Intent intent = new Intent("custom-event"); intent.putExtra("message", message); LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent); } } In the above code, we first created an Intent named Custom-Event, and added messages to send to the additional data to the INTENT.Then, we obtained an instance of LocalBroidCastManager and used the Sendbroadcast () method to send messages. Next, we will create a class called MessageReceiver, which is responsible for receiving messages.code show as below: import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.support.v4.content.LocalBroadcastManager; import android.widget.Toast; public class MessageReceiver extends BroadcastReceiver { private Context mContext; public MessageReceiver(Context context) { mContext = context; } @Override public void onReceive(Context context, Intent intent) { String message = intent.getStringExtra("message"); Toast.makeText(mContext, "Received Message: " + message, Toast.LENGTH_SHORT).show(); } } In the above code, we created a Broadcastreceiver subclass called MessageReceiver.It rewritten the onReceive () method, which was called when receiving the message.We get additional messages from the receiving Intent and display it with Toast. In order to enable the receiver to receive the message, we need to register MessageReceiver in the MainActivity (or other components that want to receive messages).code show as below: import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { private MessageReceiver mMessageReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mMessageReceiver = new MessageReceiver(this); LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("custom-event")); MessageSender messageSender = new MessageSender(this); messageSender.sendMessage("Hello World!"); } @Override protected void onDestroy() { super.onDestroy(); LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver); } } In the above code, we first created an instance of MessageReceiver, and used LocalBroadCastManager to register it as the receiver.We specify the type of message to be received through the IntENTFILTER.In MainActivity's oncreate () method, we also created an instance of MessageSender and using the SendMessage () method to send a message. Finally, in the onDestroy () method of MaainActivity, we use LocalBroidCastManager's unregisterReceiver () method to cancel the registered MessageReceiver. When we run the application, the MessageReceiver will receive a message from MessageSender and display it on the screen. To sum up, the Local Broadcast Manager framework in the Android Support library is a powerful tool for making message transmission inside the application.It implements communication between components through Publish-Subscribe design mode.Developers can send and receive messages by sending and receiving components, without using the system to broadcast the global broadcast.This provides applications with an efficient, reliable and more private way of communication.