Android supports the technical principles and usage of local radio managers (Technical Principles and USAGE of Android Support Library Broadcast Manager)
Android supports the technical principles and usage of the local radio manager of the library
In Android application development, sometimes we need to communicate between application components within the application.Under normal circumstances, we can use broadcasting to achieve this communication.Message transmission allows different components in the application to achieve data sharing and event triggers.Android support libraries provide a local broadcast manager (Local Broadcast Manager) to manage the application of internal broadcast communications more easily.
Technical principle:
1. Using a local broadcast manager can prevent the radio issued by the application from being accepted by other malicious applications.Local broadcasts are only spread within the application and will not leave the process of application.
2. Local broadcasts have higher security because it does not expose sensitive information to other applications.
Instructions:
1. Introduction dependencies:
To use the local radio manager of the Android support library, you need to add the following dependencies to the project's built.gradle file:
groovy
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.1.0'
2. Registration receiver:
In the component (such as Activity or Fragment) that needs to be received, you need to create a broadcast receiver class and register.
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Treat the receiving broadcast here
}
}
// Register a receiver
MyBroadcastReceiver myReceiver = new MyBroadcastReceiver();
LocalBroadcastManager.getInstance(context).registerReceiver(myReceiver, new IntentFilter("my_action"));
3. Send broadcast:
In the component of the sending broadcast, a broadcast can be sent through a local broadcast manager.
// Send broadcast
Intent intent = new Intent("my_action");
Intent.putextra ("Message", "Hello, World!"); // You can pass additional data
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
4. Receive the broadcast:
In the component received, the registered broadcast receiver's `onreceive () method will be called.
@Override
public void onReceive(Context context, Intent intent) {
if ("my_action".equals(intent.getAction())) {
String message = impit.getstringExtra ("Message"); // Get the data passed
// Treat the receiving broadcast here
}
}
5. Logging out the receiver:
When you no longer use the broadcast receiver, you need to cancel it.
LocalBroadcastManager.getInstance(context).unregisterReceiver(myReceiver);
By using the local radio manager of the Android support library, we can more conveniently implement the internal component communication and ensure the security and reliability of communication.
Note: Local broadcast managers are only applicable to communication between internal components. If cross -application communication is required, global broadcasting is needed.