1.1 IPC通信梳理(三)

要跟上刚哥的小弟们的步伐了, 少打游戏多读书
本文参考: 《Android开发艺术探索》

Binder连接池

假如我们的应用,有很多模块,而每一个模块都需要和服务端通讯,那么我们也要为每一个模块创建特定的aidl文件,那么服务端service也会产生很多个,显然,如果aidl接口变多,那么service也会跟着变多,打开进程管理就会暴露出一大堆的服务。所以我们可以定义一个连接池,来复用同一个服务,根据具体传入的code来返回不同的Binder对象。
这就类似于MOORs项目中的IntentRouter,集中管理,分别派发。


开始写代码

  1. 定义两个(多个)AIDL文件
    IComputer.aidl 跨进程的加法接口
// IComputer.aidl
package ziye.skintest;

interface IComputer {
   int add(int a, int b);
}

ISecurityCenter .aidl跨进程加解密接口

// ISecurityCenter.aidl
package ziye.skintest;

interface ISecurityCenter {
    String encrypt(String content);
    String decrypt(String password);
}
  1. 定义接口的实现类. 我们之前在使用AIDL文件时, 总是在服务端中以匿名内部类的形式或者接口变量。这里该规范一些了,同时也方便返回Binder对象。
    ComputeImpl.java跨进程的加法实现类
package ziye.impl;

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

SecurityCenterImpl.java跨进程的加解密实现类

public class SecurityCenterImpl extends ISecurityCenter.Stub {
    private static final char SECRET_CODE = 'w';

    @Override
    public String encrypt(String content) throws RemoteException {
        char[] chars = content.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            chars[i] ^= SECRET_CODE;
        }
        return new String(chars);
    }

    @Override
    public String decrypt(String password) throws RemoteException {
        return encrypt(password);
    }
}
  1. 编写IBinderPool.aidl,提供查询的跨进程接口,同时编写BinderPool类,使用IBinderPool的实现类来实现具体的查找方法,同时提供单例的类对象来获取具体的Binder对象。在初始化时,由池来链接服务。
// IBinderPool.aidl
package ziye.skintest;

// binder连接池  池接口
interface IBinderPool {
   IBinder queryBinder(int code);
}

这里在ServiceConnection的创建中使用到了CountDownLatch , 可以控制异步线程的顺序. 我们在实例化单例的时候,要等待bindService执行完成 , 而BinderService中要用到mBinderPoolConnection的实例化,在实例化后才会给mBinderPool赋值,进而让同步锁-1。当同步锁=0时,开始放行被阻塞的线程,这样也就顺利的完成实例化的过程,同时mBinderPool也被实例化,之后的query方法就不会遇到空指针了。

public class BinderPool {
    private static final String TAG =BinderPool.class.getSimpleName();

    //Binder具体的标识符
    public static final int BINDER_COMPUTE = 0; 
    public static final int BINDER_SECURITY_CENTER = 1;

    private IBinderPool mBinderPool;
    // 单例对象
    private static volatile BinderPool sInstance;
    private Context mContext;
    private CountDownLatch mCountDownLatch; // 同步机制

    private BinderPool(Context context) {
        mContext = context.getApplicationContext();
        //在池的初始化中链接服务,通过池可以直接返回相应的Binder对象
        connectBinderPoolService();
    }

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

    // 连接服务池
    private synchronized void connectBinderPoolService() {
        mCountDownLatch = new CountDownLatch(1); // 只保持一个绑定服务
        Intent service = new Intent(mContext, BindPoolService.class);
        mContext.bindService(service, mBinderPoolConnection, Context.BIND_AUTO_CREATE);
        try {
            //主线程在启用子线程后启用恢复机制 , 这里主线程等待connection的建立以及mBinderPool 的初始化
            mCountDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    // 失效重联机制, 当Binder死亡时, 重新连接
    private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {
        @Override public void binderDied() {
            Log.e(TAG, "Binder失效");
            mBinderPool.asBinder().unlinkToDeath(mDeathRecipient, 0);
            mBinderPool = null;
            connectBinderPoolService();
        }
    };

    // Binder的服务连接
    private ServiceConnection mBinderPoolConnection = new ServiceConnection() {
        @Override public void onServiceConnected(ComponentName name, IBinder service) {
            mBinderPool = IBinderPool.Stub.asInterface(service);
            try {
                mBinderPool.asBinder().linkToDeath(mDeathRecipient, 0);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            // connection建立完成, 通知同步锁等待线程-1
            mCountDownLatch.countDown();
        }

        @Override public void onServiceDisconnected(ComponentName name) {

        }
    };

    /**
     * 查询Binder
     * @param binderCode binder代码
     * @return Binder
     */
    public IBinder queryBinder(int binderCode) {
        IBinder binder = null;
        try {
            // 这里首先判断了mBinderPool是否为空. 因为mBinderPool是ServiceConnection
            // 实例化完成后才赋值完成的 ,所以这里判空是为了防止实例化没完成造成空指针异常
            if (mBinderPool != null) {
                binder = mBinderPool.queryBinder(binderCode);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return binder;
    }

    /**
     * Binder池的接口实现类
     */
    public static class BinderPoolImpl extends IBinderPool.Stub {
        public BinderPoolImpl() {
            super();
        }

        @Override public IBinder queryBinder(int binderCode) throws RemoteException {
            IBinder binder = null;
            switch (binderCode) {
                case BINDER_COMPUTE:
                    binder = new ComputeImpl();
                    break;
                case BINDER_SECURITY_CENTER:
                    binder = new SecurityCenterImpl();
                    break;
                default:
                    break;
            }
            return binder;
        }
    }
}
  1. 编写Service,返回一个IBinderPool的对象
public class BindPoolService extends Service{
    private static final String TAG = BindPoolService.class.getSimpleName();
    private Binder mBinderPool = new BinderPool.BinderPoolImpl();

    @Nullable @Override public IBinder onBind(Intent intent) {
        Log.e(TAG, "onBind");
        return mBinderPool;
    }
}
  1. 在Activity中测试
public class BinderPoolActivity extends Activity {
    private ISecurityCenter mISecurityCenter;
    private IComputer mICompute;

    private TextView mTvEncryptMsg; // 加密数据的显示
    private TextView mTvAddMsg; // 累计数据的显示

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    mTvEncryptMsg.setText((String) msg.obj);
                    break;
                case 1:
                    mTvAddMsg.setText((String) msg.obj);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_binderpool);

        mTvEncryptMsg = findViewById(R.id.main_tv_encrypt_msg);
        mTvAddMsg = findViewById(R.id.main_tv_add_msg);
    }

    /**
     * 加密解密的点击回调
     * @param view 界面
     */
    public void encryptMsg(View view) {
        new Thread(new Runnable() {
            @Override public void run() {
                doEncrypt();
            }
        }).start();
    }

    /**
     * 调用加密服务
     */
    private void doEncrypt() {
        BinderPool binderPool = BinderPool.getInstance(getApplicationContext());
        IBinder securityBinder = binderPool.queryBinder(BinderPool.BINDER_SECURITY_CENTER);
        mISecurityCenter = SecurityCenterImpl.asInterface(securityBinder);
        String msg = "Hello, I am Spike!";
        try {
            String encryptMsg = mISecurityCenter.encrypt(msg);
            Log.e(TAG, "加密信息: " + encryptMsg);
            String decryptMsg = mISecurityCenter.decrypt(encryptMsg);
            Log.e(TAG, "解密信息: " + decryptMsg);
            Message hm = new Message();
            hm.what = 0;
            hm.obj = encryptMsg + "\n" + decryptMsg;
            mHandler.sendMessage(hm);

        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 加法的点击回调
     * @param view 视图
     */
    public void addNumbers(View view) {
        new Thread(new Runnable() {
            @Override public void run() {
                doAddition();
            }
        }).start();
    }

    /**
     * 调用加法服务
     */
    private void doAddition() {
        BinderPool binderPool = BinderPool.getInstance(getApplicationContext());
        IBinder computeBinder = binderPool.queryBinder(BinderPool.BINDER_COMPUTE);
        mICompute = ComputeImpl.asInterface(computeBinder);
        try {
            int result = mICompute.add(12, 12);
            Log.e(TAG, "12 + 12 = " + result);

            Message hm = new Message();
            hm.what = 1;
            hm.obj = result + "";
            mHandl r.sendMessage(hm);
        } catch (RemoteException e) {
            e.printStackTrace(); 
        }
    }
}

测试中,每次调用远程服务都需要在子线程中进行,一方面是远程服务可能存在耗时操作,另一个层面来讲是为了让单例获取的时候,在子线程中进行加锁阻塞。然后调用远程服务时,利用BinderPool获取对应的binder对象,再讲binder转化为具体的接口实现(感觉有点像强制类型转换),之后再调用对应接口的方法。

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

推荐阅读更多精彩内容