前言
現(xiàn)在考慮一個(gè)情況,公司的項(xiàng)目越做越大了,有很多不同功能的業(yè)務(wù)模塊需要使用AIDL來(lái)進(jìn)行通信,如果還是按照原來(lái)的方式蹬敲,則需要?jiǎng)?chuàng)建很多service,這明顯不符合多態(tài)的特性莺戒。像寫接口一樣伴嗡,找出這些AIDL的共同點(diǎn)做出一個(gè)接口,那么AIDL的共同點(diǎn)就是他們底層都是用binder來(lái)通信的从铲。
我們將這些binder管理起來(lái)瘪校,通過(guò)接口來(lái)選擇性調(diào)用。當(dāng)然這個(gè)接口肯定是AIDL文件食店。
好渣淤,現(xiàn)在先建兩個(gè)業(yè)務(wù)模塊
// ISecurityCenter.aidl
package xxxxxxxxxxx;
interface ISecurityCenter {
String encrypt(String content);
String decrypt(String password);
}
// ICompute.aidl
package com.example.myapplication;
// Declare any non-default types here with import statements
interface ICompute {
int add(int a, int b);
}
和一個(gè)管理binder的AIDL接口
interface IBinderPool {
IBinder queryBinder(int binderCode);
}
接著赏寇,實(shí)現(xiàn)以上三個(gè)aidl的具體業(yè)務(wù)邏輯
public class ComputeImpl extends ICompute.Stub{
@Override
public int add(int a, int b) throws RemoteException {
return a + b;
}
}
public class SecurityCenterImpl extends ISecurityCenter.Stub{
private static final char SECRET_CODE = '^';
@Override
public String encrypt(String content) throws RemoteException {
return content+"haha";
}
@Override
public String decrypt(String password) throws RemoteException {
return password.toLowerCase();
}
}
public class BinderPoolImpl extends IBinderPool.Stub {
@Override
public IBinder queryBinder(int binderCode) throws RemoteException {
IBinder binder=null;
switch (binderCode){
case 1:
binder=new SecurityCenterImpl();
break;
case 2:
binder=new ComputeImpl();
break;
default:
break;
}
return binder;
}
}
創(chuàng)建一個(gè)binder連接池類吉嫩。在它的內(nèi)部,首先要去綁定遠(yuǎn)程服務(wù)嗅定,綁定成功后自娩,客戶端就可以通過(guò)useAidlByCode方法去獲取各自對(duì)應(yīng)的binder,拿到所需得到binder以后,不同業(yè)務(wù)模塊就可以進(jìn)行各自的操作忙迁。
public class BinderPoolUtil {
private Context mcontext;
private static volatile BinderPoolUtil sInstance;
private CountDownLatch mCountDownLatch;
private IBinderPool binderPoolimpl=new BinderPoolImpl();
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
binderPoolimpl = IBinderPool.Stub.asInterface(iBinder);
try {
binderPoolimpl.asBinder().linkToDeath(mDeathRecipient,0);
} catch (RemoteException e) {
e.printStackTrace();
}
mCountDownLatch.countDown();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
private BinderPoolUtil(Context mcontext) {
this.mcontext = mcontext;
useThisBindService();
}
public static BinderPoolUtil getInstance (Context context){
if(sInstance==null){
synchronized (BinderPoolUtil.class){
if(sInstance==null){
sInstance=new BinderPoolUtil(context);
}
}
}
return sInstance;
}
//使用這個(gè)類幫A綁定服務(wù)了
private synchronized void useThisBindService(){
mCountDownLatch = new CountDownLatch(1);
Intent intent = new Intent(mcontext, MyService.class);
mcontext.bindService(intent, mServiceConnection,Context.BIND_AUTO_CREATE);
try {
mCountDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 通過(guò)code來(lái)調(diào)用不同的Aidl模塊功能
* @param binderCode
* @return
*/
public IBinder useAidlByCode(int binderCode){
IBinder binder=null;
try {
if(binderPoolimpl!=null) {
binder = binderPoolimpl.queryBinder(binderCode);
}
} catch (RemoteException e) {
e.printStackTrace();
}
return binder;
}
private IBinder.DeathRecipient mDeathRecipient=new IBinder.DeathRecipient(){
@Override
public void binderDied() {
binderPoolimpl.asBinder().unlinkToDeath(this,0);
binderPoolimpl=null;
useThisBindService();
}
};
}
服務(wù)的代碼則簡(jiǎn)單
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
private BinderPoolImpl stub=new BinderPoolImpl();
@Nullable
@Override
public IBinder onBind(Intent intent) {
return stub;
}
}
然后在界面中調(diào)用binder連接池方法類查詢
public class MainActivity extends AppCompatActivity {
private BinderPoolUtil binderPoolUtil;
private IBinder scbinder;
private IBinder cibinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
@Override
public void run() {
try {
binderPoolUtil = BinderPoolUtil.getInstance(MainActivity.this);
scbinder = binderPoolUtil.useAidlByCode(1);
cibinder = binderPoolUtil.useAidlByCode(2);
ISecurityCenter iSecurityCenter = SecurityCenterImpl.asInterface(scbinder);
ICompute iCompute = ComputeImpl.asInterface(cibinder);
String s1 = iSecurityCenter.encrypt("sfeMHJI");
String s2 = iSecurityCenter.decrypt("sfeMHJI");
int add = iCompute.add(1, 2);
Log.e("yjm",s1+"--------"+s2+"--------"+add);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}).start();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}