IPC-Binder连接池

来源:互联网 发布:java面向对象基础 编辑:程序博客网 时间:2024/05/22 08:18
池的这个概念是典型的享元设计模式。比如数据库连接池,线程池。对于有限的资源,我们用一个池去管理,让资源共享于不现调用者之间。这里Binder连接池就是为了解决需要多个aidl服务时,我们可以把这些aidl接口放在池中,让service直接提供池的接口,而在客户端同样用一个池的处理类去与service绑定,调用者根据需求去操控池去调用不同接口既可。

艺术探索这本书中把服务端与调用端写在一起,特别是BinderPool这部分的客户端与服务端部分耦合在一起,这里做下分离及改进。@singwhatiwanna
实现步骤:
服务端
1. 提供被调用的aidl接口
2. 实现一个BinderPool池的aidl接口,里面有一个queryBinder方法
3. 实现上述的aidl接口
4. Service返回 BinderPoolImpl

客户端    1.客户端实现一个调用池BindPool,绑定服务,并提供queryBinder方法返回对应的binder.    2.客户端Activity操纵BindPool调用不现的aidl

具体实现如下,首选 是两个aidl接口

package mytest.jiang.wei.ipc.bindpool;interface ICompute {    int add(int a, int b);}package mytest.jiang.wei.ipc.bindpool;interface ISecurityCenter {    String encrypt(String content);    String decrypt(String password);}

IBinderPool,返回值是aidl本身

package mytest.jiang.wei.ipc.bindpool;import android.os.IBinder;interface IBindPool {    IBinder queryBind(int binderCode);}BinderPoolImpl,public class BinderPoolImpl extends IBindPool.Stub {    @Override    public IBinder queryBind(int binderCode) throws RemoteException {        IBinder binder = null;        switch (binderCode) {            case BINDER_SECURITY_CENTER :                binder = new SecurityCenterImpl();                break;            case BIND_COMPUTE:                binder = new ComputeImpl();                break;            default:                break;        }        return binder;    }}

Service中返回BinderPoolImpl

public class BinderPoolService extends Service{    private Binder mBinderPool = new BinderPool.BinderPoolImpl();    @Nullable    @Override    public IBinder onBind(Intent intent) {        return mBinderPool;    }}

下面看Client端,这里新建另外一个Module, BinderPool一般从简,我去掉了监听连接状态的部分,注意这里一定要提供unBind的方法,

public class BinderPool {    private final Context mContext;    private static volatile BinderPool instantce;    private static IBindPool mBinderPool;    private BinderPool(Context context) {        mContext = context;        connectBinderPoolService();    }    public static  BinderPool getInstance(Context context) {        if (instantce == null) {            synchronized (BinderPool.class) {                if (instantce == null) {                    instantce = new BinderPool(context);                }            }        }        return instantce;    }    private void connectBinderPoolService() {        Intent service = new Intent("com.wei.jiang.binderpool");        //5.0后的service必须以显式意图去调用,这里通过设置包名转为显式意图        service.setPackage("mytest.jiang.wei.ipc");        mContext.bindService(service, mBinderPoolConnction, Context.BIND_AUTO_CREATE);    }    public IBinder queryBinder(int binderCode) {        IBinder binder = null;        if (mBinderPool != null) {            try {                binder = mBinderPool.queryBind(binderCode);            } catch (RemoteException e) {                e.printStackTrace();            }        }        return binder ;    }    private ServiceConnection mBinderPoolConnction = new ServiceConnection() {        @Override        public void onServiceConnected(ComponentName name, IBinder service) {            mBinderPool = IBindPool.Stub.asInterface(service);        }        @Override        public void onServiceDisconnected(ComponentName name) {        }    };    public void unBind() {        mContext.unbindService(mBinderPoolConnction);    }}

Activity,在onDestory中去解绑服务,不然会报错
public class MainActivity extends Activity{

 private BinderPool binderPool;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        new Thread(new Runnable() {            @Override            public void run() {                doWork();            }        }).start();    }    private void doWork() {        binderPool = BinderPool.getInstance(this);        IBinder securityBinder = binderPool.queryBinder(BinderPool.BINDER_SECURITY_CENTER);        ISecurityCenter securityCenter = ISecurityCenter.Stub.asInterface(securityBinder);        IBinder computeBinder = binderPool.queryBinder(BinderPool.BIND_COMPUTE);        ICompute computeImpl = ICompute.Stub.asInterface(computeBinder);        String msg = "hello world";        System.out.println(msg);        try {            String password= securityCenter.encrypt(msg);            System.out.println(password);            System.out.println(securityCenter.decrypt(password));            System.out.println(computeImpl.add(1, 2));        } catch (RemoteException e) {            e.printStackTrace();        }    }    @Override    protected void onDestroy() {        super.onDestroy();        binderPool.unBind();    }}
1 0
原创粉丝点击