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();
}

}

你可能感兴趣的:(android,binder,AIDL,Service)