Detailed introduction to the technical principles of Android support library local radio managers

The Android support library provides a local broadcast manager, which is a lightweight library for spreading broadcast messages within the application.The local broadcast manager also provides a safe way, only the components in the application can receive the broadcast message.This article will introduce the technical principles of the Android support library local radio manager and some Java code examples. In Android, broadcasting is a mechanism for transmission messages to transmit messages in the app (local broadcast) or between (global broadcasting) within the application.Applications can send broadcast messages, and the receiver (also known as the broadcast receiver) can register and process these broadcast messages.However, global broadcasting may have some security risks, because in addition to application components, other applications can also receive these broadcasts.To avoid this, the Android support library provides a local broadcast manager that allows only components in the application to receive broadcast messages. The principle of the local broadcast manager is as follows: 1. Internal implementation: The local broadcast manager uses the "singles mode" and is private through the constructor to ensure that it can only be obtained through static methods.There is only a local radio manager example that can be shared global. The following is an example of a simple local radio manager: public class LocalBroadcastManager { private static LocalBroadcastManager sInstance; private Context mAppContext; private final HashMap<BroadcastReceiver, ArrayList<IntentFilter>> mReceivers = new HashMap<>(); private final HashMap<String, ArrayList<ReceiverRecord>> mActions = new HashMap<>(); // Private structure function private LocalBroadcastManager(Context context) { mAppContext = context.getApplicationContext(); } // Obtain examples of local broadcast manager public static LocalBroadcastManager getInstance(Context context) { if (sInstance == null) { synchronized (LocalBroadcastManager.class) { if (sInstance == null) { sInstance = new LocalBroadcastManager(context); } } } return sInstance; } // Register a broadcast receiver public void registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { ArrayList<IntentFilter> filters = mReceivers.get(receiver); if (filters == null) { filters = new ArrayList<>(); mReceivers.put(receiver, filters); } filters.add(filter); for (int i = 0; i < filter.countActions(); i++) { String action = filter.getAction(i); ArrayList<ReceiverRecord> receivers = mActions.get(action); if (receivers == null) { receivers = new ArrayList<>(); mActions.put(action, receivers); } receivers.add(new ReceiverRecord(receiver, filter)); } } // Solution registered broadcast receiver public void unregisterReceiver(BroadcastReceiver receiver) { ArrayList<IntentFilter> filters = mReceivers.get(receiver); if (filters != null) { for (IntentFilter filter : filters) { for (int i = 0; i < filter.countActions(); i++) { String action = filter.getAction(i); ArrayList<ReceiverRecord> receivers = mActions.get(action); if (receivers != null) { for (ReceiverRecord record : receivers) { if (record.receiver == receiver) { receivers.remove(record); break; } } if (receivers.size() == 0) { mActions.remove(action); } } } } mReceivers.remove(receiver); } } // Send local broadcast public boolean sendBroadcast(Intent intent) { String action = intent.getAction(); ArrayList<ReceiverRecord> receivers = mActions.get(action); if (receivers != null) { for (ReceiverRecord record : receivers) { record.receiver.onReceive(mAppContext, intent); } return true; } return false; } // Broadcast receiver record, including the receiver and its corresponding intention filter private static class ReceiverRecord { final BroadcastReceiver receiver; final IntentFilter filter; ReceiverRecord(BroadcastReceiver receiver, IntentFilter filter) { this.receiver = receiver; this.filter = filter; } } } 2. Registration and Resolution: Developers can register a broadcast receiver using the method of `registerReceiver ()` and use the method of `UnregisterReceiver ()` to solve the registration.When the receiver is registered, the local broadcast manager associates with each broadcast operation that the receiver is interested in. 3. Filter: Broadcast receiver can receive broadcast messages interested in the action and data screening specified in the `IntentFilter`.When calling the method of calling `registerReceiver ()`, the local broadcast manager associates the receiver with each specified action. 4. Send broadcast: Developers can send broadcast messages using `Sendbroadcast ()` method.The local broadcast manager will find the intention filter and operation of all registered receivers, and pass the broadcast message to the matching receiver. Summarize: The local broadcast manager uses the local broadcast manager class of the "singles mode" and uses HashMap inside to maintain the correlation between the receiver and the broadcast message.By using a filter, the receiver can be associated with the broadcast operations they care about.Developers can use this mechanism to convey messages in the application and ensure that the message is only sent to components of this application, increasing security.