2.5 Binder连接池

1. aidl实现流程概述

首先建立一个aidl接口和一个Service,接着实现一个类A继承aidl接口中的Stub类并实现其中的方法,在Service绑定时返回类A的对象,然后客户端就可以绑定服务端,建立连接后就可以访问远程服务端的方法了。

2. 可能出现的问题及解决方式

公司项目越来越大,100个aidl,按照上面的思路,得100个Service。这显然不可以,解决方式是binder 连接池。
整个工作机制是这样的:每个业务模块创建自己的aidl接口并实现此接口,这个时候不同业务模块之间是不能有耦合的,然后向服务端提供自己的唯一标识和其对应的Binder对象;对于服务端来说,只需要一个Service,服务端提供一个queryBinder接口,这个接口能够根据业务模块特征来返回相应的Binder对象给他们,不同的业务模块拿到所需的Binder对象后就可以进行远程方法调用了。
Binder连接池的主要作用就是将每个业务模块的Binder请求同一转发到远程Service中去,避免重复创建Service的过程。

3. 定义两个业务aidl接口并实现

package qingfengmy.developmentofart._2activity.binderpool.aidl;

interface ICompute {
    int add(int a, int b);
}
// ISecurityCenter.aidl
package qingfengmy.developmentofart._2activity.binderpool.aidl;

interface ISecurityCenter {
    String encrypt(in String content);
    String decrypt(in String password);
}
public class SecurityCenterImpl extends ISecurityCenter.Stub {
    @Override
    public String encrypt(String content) throws RemoteException {
        char[] chars = content.toCharArray();
        for (int i=0; i<chars.length;i++) {
            chars[i] = (char) (chars[i]+1);
        }

        return new String(chars);
    }

    @Override
    public String decrypt(String password) throws RemoteException {
        char[] chars = password.toCharArray();
        for (int i=0; i<chars.length;i++) {
            chars[i] = (char) (chars[i]-1);
        }

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

4. 定义BinderPool接口并实现

package qingfengmy.developmentofart._2activity.binderpool.aidl;

interface IBinderPool {
    IBinder queryBinder(int binderCode);
}
public static class BinderPoolImpl extends IBinderPool.Stub{

    @Override
    public IBinder queryBinder(int binderCode) throws RemoteException {
        switch (binderCode) {
            case 0:
                return new SecurityCenterImpl();
            case 1:
                return new ComputerImpl();
        }
        return null;
    }
}

5. 服务实现

public class PoolService extends Service {
    private Binder mBinderPool = new BinderPool.BinderPoolImpl();

    public PoolService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinderPool;
    }

}

6. BinderPool的实现

public class BinderPool {

    // 定义BinderCode
    private static final int BINDER_NONE = -1;
    public static final int BINDER_COMPUTE = 0;
    public static final int BINDER_SECURITY_CENTER = 1;

    private Context mContext;
    private IBinderPool mBinderPool;
    // volatile 用来修饰被不同线程访问和修改的变量
    private static volatile BinderPool sInstance;
    /**
     * CountDownLatch类是一个同步计数器,构造时传入int参数,该参数就是计数器的初始值,每调用一次countDown()方法,计数器减1,计数器大于0 时,await()方法会阻塞程序继续执行
     * CountDownLatch如其所写,是一个倒计数的锁存器,当计数减至0时触发特定的事件。利用这种特性,可以让主线程等待子线程的结束。
     */
    private CountDownLatch mConnectBinderPoolCountDownLatch;

    private BinderPool(Context context) {
        mContext = context.getApplicationContext();
        connectBinderPoolService();
    }

    public static BinderPool getsInstance(Context context) {
        if (sInstance == null) {
            synchronized (BinderPool.class) {
                if (sInstance == null) {
                    sInstance = new BinderPool(context);
                }
            }
        }
        return sInstance;
    }

    public IBinder queryBinder(int binderCode) {
        try {
            return mBinderPool.queryBinder(binderCode);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    private synchronized void connectBinderPoolService() {
        // 只有一个线程有效
        mConnectBinderPoolCountDownLatch = new CountDownLatch(1);
        Intent intent = new Intent(mContext, PoolService.class);
        mContext.bindService(intent, mBinderPoolConnection, Context.BIND_AUTO_CREATE);
        // 等待,直到CountDownLatch中的线程数为0
        mConnectBinderPoolCountDownLatch.await();
    }

    private ServiceConnection mBinderPoolConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBinderPool = IBinderPool.Stub.asInterface(service);
            mBinderPool.asBinder().linkToDeath(mBinderPoolDeathRecipient, 0);
            // 执行一次countDown,其计数减一
            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 static class BinderPoolImpl extends IBinderPool.Stub {

        @Override
        public IBinder queryBinder(int binderCode) throws RemoteException {
            switch (binderCode) {
                case BINDER_SECURITY_CENTER:
                    return new SecurityCenterImpl();
                case BINDER_COMPUTE:
                    return new ComputerImpl();
            }
            return null;
        }
    }
}

7. Activity中调用

private void doWork() {
    BinderPool binderPool = BinderPool.getsInstance(this);
    IBinder computeBinder = binderPool.queryBinder(BinderPool.BINDER_COMPUTE);
    ICompute compute = ComputerImpl.asInterface(computeBinder);
    try {
        int result = compute.add(1, 2);
        Log.e("aaa", "1+2=" + result);
    } catch (RemoteException e) {
        e.printStackTrace();
    }

    IBinder securityCenterBinder = binderPool.queryBinder(BinderPool.BINDER_SECURITY_CENTER);
    ISecurityCenter iSecurityCenter = SecurityCenterImpl.asInterface(securityCenterBinder);
    try {
        String content = "i love this book";
        Log.e("aaa","加密前:"+content);
        content = iSecurityCenter.encrypt(content);
        Log.e("aaa","加密后:"+content);
        content = iSecurityCenter.decrypt(content);
        Log.e("aaa","解密后:"+content);
    } catch (RemoteException e) {
        e.printStackTrace();
    }

}

普通写法

public void onServiceConnected(ComponentName name, IBinder service) {
    iBookManager = IBookManager.Stub.asInterface(service);
}

可见使用BinderPool客户端中的处理还是一样,通过Stub的asInterface方法把IBinder转为业务接口。
这样有新业务aidl时,只需加aidl_code,并在queryBinder中增加返回的aidl即可。不需写Servicce,所以如果使用aidl,必须推荐使用BinderPool模式。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,509评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,806评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,875评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,441评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,488评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,365评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,190评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,062评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,500评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,706评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,834评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,559评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,167评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,779评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,912评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,958评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,779评论 2 354

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,654评论 18 139
  • Jianwei's blog 首页 分类 关于 归档 标签 巧用Android多进程,微信,微博等主流App都在用...
    justCode_阅读 5,915评论 1 23
  • 毫不夸张地说,Binder是Android系统中最重要的特性之一;正如其名“粘合剂”所喻,它是系统间各个组件的桥梁...
    weishu阅读 17,866评论 29 246
  • Android跨进程通信IPC整体内容如下 1、Android跨进程通信IPC之1——Linux基础2、Andro...
    隔壁老李头阅读 10,757评论 13 43
  • 我对天空说 你的深邃苍茫 是我终日仰望的高处 我对大地说 你的广袤无垠 是我踏之不遍的长度 我对大海说 你的幽暗无...
    明_熙阅读 333评论 12 26