实现AIDL接口的Binder连接池
Binder作為AIDL通信的核心, 在使用中經常需要重復利用, 動態管理AIDL接口. Binder連接池的主要作用是把Binder請求統一發送至Service執行, 即動態管理Binder操作, 避免重復創建Service. 本文使用兩種簡單的AIDL服務, 使用Binder連接池動態切換, 含有演示Demo.
Binder
本文源碼的GitHub下載地址
AIDL
模擬Binder連接池, 使用兩個簡單的AIDL接口與實現, 一個是加解密, 一個是加法.
加解密, AIDL提供兩個方法, 即加密字符串和解密字符串.
package org.wangchenlong.binderpooldemo;interface ISecurityCenter {String encrypt(String content);String decrypt(String password); }加密和解密的實現, 使用簡單的異或運算處理.
public class SecurityCenterImpl extends ISecurityCenter.Stub {private static final char SECRET_CODE = 'w'; 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);} public String decrypt(String password) throws RemoteException {return encrypt(password);} }AIDL的實現方法都需要設置RemoteException的異常拋出, 防止連接異常.
求和的AIDL接口
package org.wangchenlong.binderpooldemo;interface ICompute {int add(int a, int b); }求和的實現, 非常簡單.
public class ComputeImpl extends ICompute.Stub { public int add(int a, int b) throws RemoteException {return a + b;} }Binder連接池通過ID查找Bidner, 查詢并返回匹配的Binder.
package org.wangchenlong.binderpooldemo;interface IBinderPool {IBinder queryBinder(int binderCode); }Binder 連接池
Service服務通過Binder連接池動態選擇Binder請求.
private Binder mBinderPool = new BinderPool.BinderPoolImpl(); // 動態選擇Binder public IBinder onBind(Intent intent) {Log.e(TAG, "onBind");return mBinderPool; }Binder連接池的具體實現, 創建BinderPool單例, 連接服務.
private BinderPool(Context context) {mContext = context.getApplicationContext();connectBinderPoolService(); // 連接服務 }public static BinderPool getInstance(Context context) {if (sInstance == null) {synchronized (BinderPool.class) {if (sInstance == null) {sInstance = new BinderPool(context);}}}return sInstance; }綁定服務, 通過CountDownLatch類, 把異步操作轉換為同步操作, 防止綁定沖突.
private synchronized void connectBinderPoolService() {mCountDownLatch = new CountDownLatch(1); // 只保持一個綁定服務Intent service = new Intent(mContext, BinderPoolService.class);mContext.bindService(service, mBinderPoolConnection, Context.BIND_AUTO_CREATE);try {mCountDownLatch.await();} catch (InterruptedException e) {e.printStackTrace();} }通過DeathRecipient處理Binder連接池死亡重聯機制.
// 失效重聯機制, 當Binder死亡時, 重新連接 private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() { public void binderDied() {Log.e(TAG, "Binder失效");mBinderPool.asBinder().unlinkToDeath(mDeathRecipient, 0);mBinderPool = null;connectBinderPoolService();} };// Binder的服務連接 private ServiceConnection mBinderPoolConnection = new ServiceConnection() { public void onServiceConnected(ComponentName name, IBinder service) {mBinderPool = IBinderPool.Stub.asInterface(service);try {mBinderPool.asBinder().linkToDeath(mDeathRecipient, 0);} catch (RemoteException e) {e.printStackTrace();}mCountDownLatch.countDown();} public void onServiceDisconnected(ComponentName name) {} };通過ID連接不同的Binder請求.
public IBinder queryBinder(int binderCode) {IBinder binder = null;try {if (mBinderPool != null) {binder = mBinderPool.queryBinder(binderCode);}} catch (RemoteException e) {e.printStackTrace();}return binder; }Binder連接池AIDL的具體實現, 通過ID選擇Binder.
public static class BinderPoolImpl extends IBinderPool.Stub {public BinderPoolImpl() {super();} 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;} }AIDL并不會直接生成, 使用AS的Build -> Make Project即可.
客戶端
通過AIDL接口, 把耗時任務移到Service進行. 操作Binder需要在其他線程中執行, 使用Handler回調至主線程, 并更新頁面.
public void encryptMsg(View view) {new Thread(new Runnable() { 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();} }其他線程使用Handler向主線程傳遞數據, 在界面中顯示效果.
加法操作類似.
public void addNumbers(View view) {new Thread(new Runnable() { 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 + "";mHandler.sendMessage(hm);} catch (RemoteException e) {e.printStackTrace();} }注意AIDL需要捕獲RemoteException的異常.
效果
效果
AIDL是較為高效的跨進程通信方式, 也是很多方式的低層實現; Binder連接池可以在同一服務中處理多個Binder請求, 節省資源, 因此需要熟練掌握.
OK, that's all! Enjoy it!
原文地址:?http://www.jianshu.com/p/39c1c2bf39c6
總結
以上是生活随笔為你收集整理的实现AIDL接口的Binder连接池的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 探索7.x, 全面解析Activity启
- 下一篇: Android进程使用Messenger