提供两个AIDL接口来模拟多个业务模块
interface ISecurityCenter { String encrypt(String content); String decrypt(String password); } interface ICompute { int add(int a,int b); }为BinderPool创建AIDL接口
interface IBinderPool { IBinder queryBinder(int binderCode); }BinderPool:
/** * 内部首先它要去绑定远程服务,绑定成功后。客户端通过queryBinder方法获得鸽子对应的Binder。拿到Binder后就可以进行各自的操作了 */ public class BinderPool { private Context mContext; 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 IBinderPool mBinderPool; private static volatile BinderPool sIntance; private CountDownLatch mConnectBinderPoolCountDownLatch; public BinderPool(Context mContext) { this.mContext = mContext; connectBinderPoolService(); } public static BinderPool getInstance(Context context){ if(sIntance==null) { synchronized (BinderPool.class){ if(sIntance==null){ sIntance= new BinderPool(context); } } } return sIntance; } private synchronized void connectBinderPoolService() { mConnectBinderPoolCountDownLatch=new CountDownLatch(1); Intent service=new Intent(mContext,BinderPoolService.class); mContext.bindService(service,mBinderPoolConnection,Context.BIND_AUTO_CREATE); try { mConnectBinderPoolCountDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } } //创建远程Service并实现IBinderPool。 public IBinder queryBinder(int binderCode){ IBinder binder=null; if (mBinderPool != null) { try { //根据不同模块的标识返回不同的Binder对象 binder=mBinderPool.queryBinder(binderCode); } catch (RemoteException e) { e.printStackTrace(); } } return binder; } private ServiceConnection mBinderPoolConnection=new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { mBinderPool=IBinderPool.Stub.asInterface(iBinder); try { mBinderPool.asBinder().linkToDeath(mBinderPoolDeathRecipient,0); } catch (RemoteException e) { e.printStackTrace(); } mConnectBinderPoolCountDownLatch.countDown(); } @Override public void onServiceDisconnected(ComponentName componentName) { } }; private IBinder.DeathRecipient mBinderPoolDeathRecipient=new IBinder.DeathRecipient() { @Override public void binderDied() { Log.e(TAG,"binderDied"); mBinderPool.asBinder().unlinkToDeath(mBinderPoolDeathRecipient,0); mBinderPool=null; connectBinderPoolService(); } }; public static class BinderPoolImpl extends IBinderPool.Stub{ @Override public IBinder queryBinder(int binderCode) throws RemoteException { IBinder binder=null; switch (binderCode){ case BINDER_SECURITY_CENTER: binder=new SecurityCenterImpl(); break; case BINDER_COMPUTE: binder=new ComputeImpl(); break; default: break; } return binder; } } }实现ICompute.AIDL
public class ComputeImpl extends ICompute.Stub{ @Override public int add(int a, int b) throws RemoteException { return a+b; } }实现SecurityCenter.AIDL
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); } }Service:
public class BinderPoolService extends Service { private static final String TAG="BinderPoolService"; private Binder mBinderPool=new BinderPool.BinderPoolImpl(); @Override public IBinder onBind(Intent intent) { Log.e(TAG,"onBind"); return mBinderPool; } @Override public void onDestroy() { super.onDestroy(); } }执行操作:
public class MainActivity extends AppCompatActivity { private String TAG="MainActivity"; private ISecurityCenter mSecurityCenter; private ICompute mCompute; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void test(View view){ new Thread(new Runnable() { @Override public void run() { doWork(); } }).start(); } public void doWork(){ BinderPool binderPool=BinderPool.getInstance(MainActivity.this); //------------------------------------------------------------------------- //通过标识获得IBinder对象 IBinder securityBinder=binderPool.queryBinder(BinderPool.BINDER_SECURITY_CENTER); mSecurityCenter = SecurityCenterImpl.asInterface(securityBinder); Log.e(TAG,"visit ISecurityCenter"); String msg="helloworld--安卓"; System.out.println("content:"+msg); try { String password=mSecurityCenter.encrypt(msg); System.out.println("encrypt:"+password); System.out.println("decrypt:"+mSecurityCenter.decrypt(password)); } catch (RemoteException e) { e.printStackTrace(); } //-------------------------------------------------------------------------- Log.e(TAG,"visit ICompute"); IBinder computeBinder=binderPool.queryBinder(BinderPool.BINDER_COMPUTE); mCompute= (ICompute)ComputeImpl.asInterface(computeBinder); try { System.out.println("3+5="+mCompute.add(3,5)); } catch (RemoteException e) { e.printStackTrace(); } } }执行结果: 有了BinderPool可以方便日常的开发工作,比如需要一个新的业务模块需要添加新的AIDL,那么在它实现自己的AIDL接口后,只需修改BinderPoolImpl的queryBinder方法,给自己添加一个新的binderCode并返回对应的Binder对象即可。不需要做其他修改,也不需要创建新的Service。