Android Service Binder连接池(二)

上一篇是一个Service管理一个AIDL,如果要实现一个Service管理多个AIDL,就需要引入Binder连接池

创建多个AIDL

interface IBinderPool {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    IBinder queryBinder(int binderCode);
}
interface FishInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void eat(int type);
}
interface CatInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void eat();
}

实现生成的Binder

public class BinderPoolImpl extends IBinderPool.Stub{
    public static final int BINDER_FISH = 1;
    public static final int BINDER_CAT = 2;

    @Override
    public IBinder queryBinder(int binderCode) throws RemoteException {
        IBinder iBinder = null;
        switch (binderCode){
            case BINDER_FISH:
                iBinder = new FishBinder();
                break;
            case BINDER_CAT:
                iBinder = new CatBinder();
        }
        return iBinder;
    }
}
public class CatBinder extends CatInterface.Stub{
    @Override
    public void eat() throws RemoteException {

    }
}
public class FishBinder extends FishInterface.Stub{
    @Override
    public void eat(int type) throws RemoteException {

    }
}

Service 添加连接池

    override fun onBind(intent: Intent): IBinder {
        return BinderPoolImpl();
    }

修改管理类

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.ParcelUuid;
import android.os.RemoteException;
import android.util.Log;


public class TestServiceManager {
    private static final String TAG = "TestServiceManager";
    private TestCallBack mTestCallBack;
    private Context mContext;
    private volatile static TestServiceManager instance;
    private IBinderPool binderPool;
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG,"===onServiceConnected==="+(service != null));
            if (service!=null){
                binderPool = IBinderPool.Stub.asInterface(service);
                if (mTestCallBack!=null){
                    mTestCallBack.success(binderPool);
                }
                try {
                    service.linkToDeath(deathRecipient,0);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        Log.i(TAG,"===onServiceDisconnected===");
        if (mTestCallBack!=null){
            mTestCallBack.fail();
        }
        binderPool = null;
    }
};
private IBinder.DeathRecipient deathRecipient=new IBinder.DeathRecipient() {
    @Override
    public void binderDied() {
        Log.i(TAG,"===binderDied===");
        if(binderPool!=null){
            binderPool.asBinder().unlinkToDeath(deathRecipient,0);
            binderPool=null;
            bind();
        }
    }
};
private TestServiceManager() {

}

public static TestServiceManager getInstance() {
    if (instance == null) {
        synchronized (TestServiceManager.class) {
            if (instance == null) {
                instance = newTestServiceManager();
            }
        }
    }
    return instance;
}

public void init(Context context, TestCallBack testCallBack) {
    Log.i(TAG,"===init===");
    mTestCallBack = testCallBack;
    mContext = context;
    bind();
}
private void bind(){
    Log.i(TAG,"===bind===");
    if (mContext!=null){
        Intent intent = new Intent("com.test");
        intent.setPackage("com.test");
        mContext.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }
}
public void release(){
    Log.i(TAG,"===release===");
    mContext.unbindService(serviceConnection);
    binderPool = null;
    instance = null;
}

public IBinder getTestBinder(int code) {
    IBinder binder = null;
    try {
        if (binderPool != null) {
            binder = binderPool.queryBinder(code);
        }
    } catch (RemoteException e) {
        e.printStackTrace();
    }
    return binder;
}

public interface TestCallBack {
    void success(ITestService service);

    void fail();
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值