AIDL连接池的实现

参考《Android开发艺术探索》学习一下AIDL的连接池实现

回顾一下AIDL使用的大致流程:首先创建一个Service和一个AIDL接口,接着创建一个类继承自AIDL接口中的Stub类并实现Stub中的抽象方法,在Service的onBind方法中返回这个类的对象,然后客户端就可以绑定服务端Service,建立连接后就可以访问远程服务端的方法了。

现在考虑一种情况:公司的项目越来越庞大了,现在有10个不同的业务模块都需要使用AIDL来进行进程间通信,那我们该怎么处理呢?也许你会说:“就按照AIDL的实现方式一个个来吧”,这是可以的,如果用这种方法,首先我们需要创建10个Service,这好像有点多啊!如果有100个地方需要用到AIDL呢,先创建100个Service?到这里,读者应该明白问题所在了。随着AIDL数量的增加,我们不能无限制地增加Service,Service是四大组件之一,本身就是一种系统资源。而且太多的Service会使得我们的应用看起来很重量级,因为正在运行的Service可以在应用详情页看到,当我们的应用详情显示有10个服务正在运行时,这看起来并不是什么好事。针对上述问题,我们需要减少Service的数量,将所有的AIDL放在同一个Service中去管理。在这种模式下,整个工作机制是这样的:每个业务模块创建自己的AIDL接口并实现此接口,这个时候不同业务模块之间是不能有耦合的,所有实现细节我们要单独开来,然后向服务端提供自己的唯一标识和其对应的Binder对象;对于服务端来说,只需要一个Service就可以了,服务端提供一个queryBinder接口,这个接口能够根据业务模块的特征来返回相应的Binder对象给它们,不同的业务模块拿到所需的Binder对象后就可以进行远程方法调用了。由此可见,Binder连接池的主要作用就是将每个业务模块的Binder请求统一转发到远程Service中去执行,从而避免了重复创建Service的过程。
1542631800(1).jpg

第一步,假设目前需求是需要两个AIDL接口,来实现加密解密和计算的功能,那么先新建ISecurityCenter.aidl 和 ICompute.aidl文件以及它们的实现类:

interface ISecurityCenter {
    String encrypt(String content);
    String decrypt(String password);
} 
interface ICompute {
    int add(int a, int b);
}

public class SecurityCenterImpl extends ISecurityCenter.Stub {
    private static final char SECRET_CODE = '^';
    @Override
    public String encrypt(String content) throws RemoteException {
        // TODO 功能实现略
        return "加密后的字符串";
    }
    @Override
    public String decrypt(String password) throws RemoteException {
        // TODO 功能实现略
        return "解密后的字符串";
    }
}

public class ComputeImpl extends ICompute.Stub {
    @Override
    public int add(int a, int b) throws RemoteException {
        return a + b;
    }
}

第二步,为Binder连接池创建IBinderPool.aidl文件并实现服务:

public class BinderPoolService extends Service {
    public static final int BINDER_COMPUTE = 0;
    public static final int BINDER_SECURITY_CENTER = 1;
 
    private Binder mBinderPool = new BinderPoolImpl();
 
    public static class BinderPoolImpl extends IBinderPool.Stub {
        @Override
        public IBinder queryBinder(int binderCode) throws RemoteException {
            IBinder binder = null;
            switch (binderCode) {
                case BINDER_SECURITY_CENTER: {
                    binder = new SecurityCenterImpl();
                    break;
                }
                case BINDER_COMPUTE: {
                    binder = new ComputeImpl();
                    break;
                }
                default:
                    break;
            }
            return binder;
        }
    }
 
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override
    public IBinder onBind(Intent intent) {
        return mBinderPool;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

public class BinderPoolService extends Service {
    public static final int BINDER_COMPUTE = 0;
    public static final int BINDER_SECURITY_CENTER = 1;
 
    private Binder mBinderPool = new BinderPoolImpl();
 
    public static class BinderPoolImpl extends IBinderPool.Stub {
        @Override
        public IBinder queryBinder(int binderCode) throws RemoteException {
            IBinder binder = null;
            switch (binderCode) {
                case BINDER_SECURITY_CENTER: {
                    binder = new SecurityCenterImpl();
                    break;
                }
                case BINDER_COMPUTE: {
                    binder = new ComputeImpl();
                    break;
                }
                default:
                    break;
            }
            return binder;
        }
    }
 
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override
    public IBinder onBind(Intent intent) {
        return mBinderPool;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

第三步,重要,新建一个BinderPool类用于专门处理Binder连接池的绑定Service和获取对应Binder对象:

public class BinderPool {
 
    // 单例
    private static volatile BinderPool sInstance;
    public static BinderPool getInsance(Context context) {
        if (sInstance == null) {
            synchronized (BinderPool.class) {
                if (sInstance == null) {
                    sInstance = new BinderPool(context);
                }
            }
        }
        return sInstance;
    }
 
    private Context mContext;
    private IBinderPool mBinderPool;
    // 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待(异步转同步)
    private CountDownLatch mConnectBinderPoolCountDownLatch;
 
    private BinderPool(Context context) {
        mContext = context.getApplicationContext();
        connectBinderPoolService();
    }
 
    // 绑定服务
    private synchronized void connectBinderPoolService() {
        mConnectBinderPoolCountDownLatch = new CountDownLatch(1);
        Intent service = new Intent(mContext, BinderPoolService.class);
        mContext.bindService(service, mBinderPoolConnection, Context.BIND_AUTO_CREATE);
        try {
            mConnectBinderPoolCountDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
 
    private ServiceConnection mBinderPoolConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // 连接上服务后,获取服务里远程提供的Binder mBinderPool对象,它是前面的IBinderPool接口
            mBinderPool = IBinderPool.Stub.asInterface(service);
            try {
                // linkToDeath可以给Binder设置一个死亡代理
                mBinderPool.asBinder().linkToDeath(mBinderPoolDeathRecipient, 0);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            mConnectBinderPoolCountDownLatch.countDown();
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
 
    private IBinder.DeathRecipient mBinderPoolDeathRecipient = new IBinder.DeathRecipient() {
        @Override
        public void binderDied() {
            mBinderPool.asBinder().unlinkToDeath(mBinderPoolDeathRecipient, 0);
            mBinderPool = null;
            // 重新绑定服务
            connectBinderPoolService();
        }
    };
 
    public IBinder queryBinder(int binderCode) {
        IBinder binder = null;
        try {
            if (mBinderPool != null) {
                binder = mBinderPool.queryBinder(binderCode);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return binder;
    }
}

1、BinderPool的实现方式是一个单例,并在构造函数中实现了Service的绑定
2、Service的绑定过程中使用了CountDownLatch来进行同步执行,以确保客户端在执行调用之前已经绑定好服务端
3、绑定Service完成后,获得一个mBinderPool对象,并为其设置一个死亡代理,使在意外断开后能重新绑定
4、对外提供queryBinder方法,通过约定的code调用mBinderPool 对象的queryBinder方法返回相应用Binder对象

第四步、使用

private void doWork() {
    BinderPool binderPool = BinderPool.getInsance(MainActivity.this);
 
    IBinder securityBinder = binderPool.queryBinder(BinderPoolService.BINDER_SECURITY_CENTER);
    mSecurityCenter = SecurityCenterImpl.asInterface(securityBinder);
    try {
        String msg = "hello ffo";
        String password = mSecurityCenter.encrypt(msg);
        String originalPassword  = mSecurityCenter.decrypt(password);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
 
    IBinder computeBinder = binderPool.queryBinder(BinderPoolService.BINDER_COMPUTE);
    mCompute = ComputeImpl.asInterface(computeBinder);
    try {
        int reuslt = mCompute.add(6, 8);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

有了BinderPool可以大大方便日常的开发工作,比如如果有一个新的业务模块需要添加新的AIDL,那么在它实现了自己的AIDL接口后,只需要修改BinderPoolImpl中的queryBinder方法,给自己添加一个新的binderCode并返回对应的Binder对象即可,不需要做其他修改,也不需要创建新的Service。由此可见,BinderPool能够极大地提高AIDL的开发效率,并且可以避免大量的Service创建,因此,建议在AIDL开发工作中引入BinderPool
机制。

猜你喜欢

转载自blog.csdn.net/flowerff/article/details/84319025
今日推荐