android-AIDL the interface registration / deregistration

Series of articles

AIDL basic use of
custom data types of AIDL
AIDL method of re-connection
AIDL the interface registration / deregistration
connection pool of AIDL

Knowledge Point

  1. Interface used between AIDL
  2. Inter AIDL process interface registration / deregistration

First, the use of the interface between AIDL

Why specifically explain the registration and cancellation of registration interface, because the process when using AIDL cross-process communication interface object passed every address in memory is not the same , so after registering, can not use conventional way to cancel, because the transfer registration registration reconciliation interface addresses are not the same , the system does not recognize

Due to the above problems, AIDL provides a class specifically address the above situation RemoteCallbackList, and its working principle is:

Map results within a callback designed to save all AIDL of the Map of Key is IBinder type, value is Callback type.
Same object although many cross-process transfer client will generate different objects on the server, but these newly generated objects have in common is that they target the underlying Binder is the same, take advantage of this feature, you can achieve the above problems encountered

Second, the interface between AIDL process registration / deregistration

First of all, in front of explaining the basic use AIDL the basis of the first interface and adding new AIDL registration reconciliation register:

import com.returntolife.jjcode.mydemolist.bean.AIDLBook;

interface IOnNewBookArrivedListener {

    void onNewsBookArrived(in AIDLBook book);
}
interface IPerson {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void  setName(String s);
    String  getName();

    void setBook(in AIDLBook book);
    AIDLBook getBook();

    void registerListener(IOnNewBookArrivedListener listener);
    void unregisterListener(IOnNewBookArrivedListener listener);
}

Then it is RemoteCallbackListto use: the
registration / de-registration is very simple

private RemoteCallbackList<IOnNewBookArrivedListener> mListener=new RemoteCallbackList<>();
   ...
   ...
        @Override
        public void registerListener(IOnNewBookArrivedListener listener) throws RemoteException {
            LogUtil.d("registerListener listener="+listener);
            mListener.register(listener);
        }

        @Override
        public void unregisterListener(IOnNewBookArrivedListener listener) throws RemoteException {
            LogUtil.d("unregisterListener listener="+listener);
            mListener.unregister(listener);
        }

Followed by the use of the interface by:

       @Override
        public void setBook(AIDLBook book) throws RemoteException {
            LogUtil.d("setBook="+book);
            mybook=book;
            //调用接口通知客户端
            if(mListener!=null){
                int size=mListener.beginBroadcast();
                for (int i = 0; i < size; i++) {
                    IOnNewBookArrivedListener listener=mListener.getBroadcastItem(i);
                    listener.onNewsBookArrived(book);
                }
                mListener.finishBroadcast();
            }
        }

ps: Note that the beginBroadcast()method and the finishBroadcast()method must be used , even if simply get set size


to sum up

Use AIDL inter cross-process communication, we often need to register to listen, let's notice the server, but the server must also provide a method of de-registration, or if the client did not want to leave an interface to accept the message, though immediately leave the client side processing method does not go wrong, but before the server side of the monitor collection still exists, it will waste system resources, so there is registered listeners, the best solution should be to achieve registration

Demo Institute Add

https://github.com/returntolife455/DemoList

Reference article

"Android development of artistic exploration."

Reproduced in: https: //www.jianshu.com/p/e80c9bc36ed0

Guess you like

Origin blog.csdn.net/weixin_34128411/article/details/91191435