[Android] RemoteCallbackList of source code

foreword

When replacing the Bluetooth solution provider recently, it was found that it was using RemoteCallbackList for callback. So take a look at this class with a learning mentality.

In Android, the RemoteCallbackList class is a tool class for cross-process communication, which can be used to register, cancel and notify the callback interface of the client. It is widely used in various system services and framework components in the Android system, such as notification services, Activity life cycle callbacks, media players, and so on.

What is RemoteCallbackList

RemoteCallbackList is a list-like data structure used to store callback interface objects. Each callback interface object has a unique Binder identifier, which can be used to transfer and track between different processes. The main function of RemoteCallbackList is to provide a set of safe API, so that the client can register, cancel and notify the callback interface object, and also provide some thread-safe methods to ensure the correctness of concurrent operations.

Use of RemoteCallbackList

When using RemoteCallbackList, we usually need to implement a Binder service or an AIDL interface, and create a RemoteCallbackList object in it. This object will be used to store the callback interface object registered by the client.

The following are common methods of RemoteCallbackList:

register(T callback):注册一个回调接口对象。
unregister(T callback):注销一个回调接口对象。
beginBroadcast():开始通知客户端,返回客户端数量。
getBroadcastItem(int index):获取指定位置的回调接口对象。
finishBroadcast():通知客户端结束。

Here is a simple example:

public class MyBinder extends Binder {
    
    
    private RemoteCallbackList<MyCallback> mCallbacks = new RemoteCallbackList<>();

    public void registerCallback(MyCallback callback) {
    
    
        mCallbacks.register(callback);
    }

    public void unregisterCallback(MyCallback callback) {
    
    
        mCallbacks.unregister(callback);
    }

    public void notifyCallbacks(String message) {
    
    
        int count = mCallbacks.beginBroadcast();
        for (int i = 0; i < count; i++) {
    
    
            try {
    
    
                mCallbacks.getBroadcastItem(i).onMessageReceived(message);
            } catch (RemoteException e) {
    
    
                // do nothing
            }
        }
        mCallbacks.finishBroadcast();
    }
}

In the above example, we created a Binder service named MyBinder, and used RemoteCallbackList to implement registration, cancellation and notification callback interface objects. When the client calls the registerCallback method, the callback interface object will be added to the RemoteCallbackList; when the client calls the unregisterCallback method, the callback interface object will be removed from the RemoteCallbackList; when the server needs to notify the client, it will call the notifyCallbacks method , traverse all the callback interface objects in the RemoteCallbackList, and call their corresponding methods to realize the callback function.

Thread safety of RemoteCallbackList

Because RemoteCallbackList may be accessed by multiple threads at the same time, its thread safety is very important. RemoteCallbackList internally uses some synchronization mechanisms to ensure the correctness of its concurrent operations.

Specifically, RemoteCallbackList internally maintains two different lists: one is the list of registered callback interfaces, and the other is the list of callback interfaces being notified. When the client registers or cancels the callback interface object, it will first acquire the lock, and then synchronize the operation to the registered callback interface list. When the server needs to notify the client, it will also acquire the lock, and then set the callback interface list being notified as a copy of the registered callback interface list, so as to ensure that the registration and cancellation of the callback interface object during the notification will not affect the notifying list.

In addition, RemoteCallbackList also provides some thread-safe methods, such as beginBroadcast, getBroadcastItem and finishBroadcast, these methods will acquire locks during the operation to ensure the correctness of concurrent operations.
Summarize

The RemoteCallbackList class is an important tool class in the Android framework, which is used for callback interface processing in cross-process communication. It provides a set of safe APIs so that clients can register, unregister and notify callback interface objects, and provides some thread-safe methods to ensure the correctness of concurrent operations. In actual development, we usually need to use RemoteCallbackList in Binder service or AIDL interface to realize registration, cancellation and notification of callback interface objects.

Guess you like

Origin blog.csdn.net/qq_24252589/article/details/131441391