要跟上刚哥的小弟们的步伐了, 少打游戏多读书
本文参考: 《Android开发艺术探索》
Binder连接池
假如我们的应用,有很多模块,而每一个模块都需要和服务端通讯,那么我们也要为每一个模块创建特定的aidl文件,那么服务端service也会产生很多个,显然,如果aidl接口变多,那么service也会跟着变多,打开进程管理就会暴露出一大堆的服务。所以我们可以定义一个连接池,来复用同一个服务,根据具体传入的code来返回不同的Binder对象。
这就类似于MOORs项目中的IntentRouter,集中管理,分别派发。
开始写代码
- 定义两个(多个)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);
}
- 定义接口的实现类. 我们之前在使用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);
}
}
- 编写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;
}
}
}
- 编写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;
}
}
- 在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转化为具体的接口实现(感觉有点像强制类型转换),之后再调用对应接口的方法。