Binder Driver浅析:Binder线程池

本文介绍了如何在Android应用中使用AIDL接口(如ISecurityCenter和ICompute)进行通信,并实现了一个简单的Binder连接池,展示了如何根据标识查询并返回相应的Binder实例。此外,还提及了Service的使用和Binder线程池的实现,以支持并发处理。
摘要由CSDN通过智能技术生成

ISecurityCenter.aidl

// ISecurityCenter.aidl

package com.breezehan.ipc.binderpool;

interface ISecurityCenter {

String encrypt(String content);

String decrypt(String password);

}

ICompute.aidl

// ICompute.aidl

package com.breezehan.ipc.binderpool;

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 {

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);

}

}

public class ComputeImpl extends ICompute.Stub {

@Override

public int add(int a, int b) throws RemoteException {

return a + b;

}

}

业务模块都有了,我们要创建一个Binder连接池需要的AIDL,这里是一个代理或工厂,根据标识返回对应Binder

// IBinderPool.aidl

package com.breezehan.ipc.binderpool;

interface IBinderPool {

IBinder queryBinder(int binderCode);

}

在连接池中实现,根据标识返回不同Binder

static class BinderPoolImpl extends IBinderPool.Stub {

@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;

}

return binder;

}

}

Service比较简单,逻辑处理都放在Binder线程池中

package com.breezehan.ipc.binderpool;

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

import android.util.Log;

public class BinderPoolService extends Service {

private static final String TAG = “BinderPoolService”;

private IBinder binderPool = new BinderPool.BinderPoolImpl();

public BinderPoolService() {

}

@Override

public IBinder onBind(Intent intent) {

Log.d(TAG, “onBind”);

return binderPool;

}

@Override

public void onDestroy() {

super.onDestroy();

}

}

Binder线程池的具体实现,同时需处理Binder死亡代理问题。

public class BinderPool {

private static final String TAG = “BinderPool”;

public static final int BINDER_NONE = -1;

public static final int BINDER_COMPUTE = 0;

public static final int BINDER_SECURITY_CENTER = 1;

private static volatile BinderPool sInstance;//确保并发取值正确性

private final Context mContext;

//控制多个线程时,某一线程中代码执行顺序;是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行

private CountDownLatch mConnectBinderPoolCountDownLatch;

private IBinderPool mBinderPool;

public BinderPool(Context context) {

mContext = context.getApplicationContext();

connectBinderPoolService();

}

private void connectBinderPoolService() {

mConnectBinderPoolCountDownLatch = new CountDownLatch(1);

Intent service = new Intent(mContext, BinderPoolService.class);

mContext.bindService(service, mServiceConnection, Context.BIND_AUTO_CREATE);

try {

mConnectBinderPoolCountDownLatch.await();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

public static BinderPool getInstance(Context context) {

if (sInstance == null) {

synchronized (BinderPool.class) {

if (sInstance == null) {

sInstance = new BinderPool(context);

}

}

}

return sInstance;

}

private ServiceConnection mServiceConnection = new ServiceConnection() {

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

mBinderPool = IBinderPool.Stub.asInterface(service);

try {

mBinderPool.asBinder().linkToDeath(mBinderDeathRecipient, 0);

} catch (RemoteException e) {

e.printStackTrace();

}

mConnectBinderPoolCountDownLatch.countDown();

}

@Override

public void onServiceDisconnected(ComponentName name) {

}

};

private IBinder.DeathRecipient mBinderDeathRecipient = new IBinder.DeathRecipient() {

@Override

public void binderDied() {

Log.w(TAG, "binderDied: ");

mBinderPool.asBinder().unlinkToDeath(mBinderDeathRecipient, 0);

mBinderPool = null;

connectBinderPoolService();

}

};

public IBinder queryBinder(int bindCode) {

IBinder binder = null;

try {

binder = mBinderPool.queryBinder(bindCode);

} catch (RemoteException e) {

e.printStackTrace();

}

return binder;

}

static class BinderPoolImpl extends IBinderPool.Stub {

@Override

public IBinder queryBinder(int binderCode) throws RemoteException {

IBinder binder =null;

switch (binderCode) {

case BINDER_COMPUTE:
先自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以扫码领取!!!!

最后

针对于上面的问题,我总结出了互联网公司Android程序员面试涉及到的绝大部分面试题及答案,并整理做成了文档,以及系统的进阶学习视频资料。
(包括Java在Android开发中应用、APP框架知识体系、高级UI、全方位性能调优,NDK开发,音视频技术,人工智能技术,跨平台技术等技术资料),希望能帮助到你面试前的复习,且找到一个好的工作,也节省大家在网上搜索资料的时间来学习。
Android进阶视频+面试资料部分截图

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可免费领取!

得这些内容对你有帮助,可以扫码领取!!!!

最后

针对于上面的问题,我总结出了互联网公司Android程序员面试涉及到的绝大部分面试题及答案,并整理做成了文档,以及系统的进阶学习视频资料。
(包括Java在Android开发中应用、APP框架知识体系、高级UI、全方位性能调优,NDK开发,音视频技术,人工智能技术,跨平台技术等技术资料),希望能帮助到你面试前的复习,且找到一个好的工作,也节省大家在网上搜索资料的时间来学习。
[外链图片转存中…(img-pTvu8JyZ-1711287646157)]

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可免费领取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值