android在一个service 中绑定多个AIDL文件

我们的项目中有时候会出现开启多个进程的情况每个进程中会有不同的逻辑,加入我们有10中逻辑就要有10个service么这种肯定不可取的,这样会浪费我们内存资源,下面介绍就是如何在一个service 中 进行多个aidl操作
直接上代码
关键代码主要在BinderPoor类中,因为我们在BinderPoor类 使用了CountDownLatch所用我们要在Activity开启线程执行
//CountDownLatch起到了同步的作用
@Route(path = MyTaskArouterConsts.Data)
public class BindActivitys extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Thread(new ServiceWorks()).start();

}

private IComput computs;
private void doWork() {
BinderPoor binderPoor = BinderPoor.getInsance(this);
IBinder binder = binderPoor.queryBinders(2);
computs = (IComput) Computs.asInterface(binder);
try {
int all = computs.add(10, 10);
System.out.println(all + “总数”);
} catch (RemoteException e) {
e.printStackTrace();
}

}

private class ServiceWorks implements Runnable{

  @Override
  public void run() {
       doWork();
  }

}

}

/**

  • 关键的辅助类
    */
    public class BinderPoor {
    public static final int BINDER_NONE = -1;
    public static final int BINDER_COMPITE = 0;
    public static final int BINDER_CEBTER = 1;

    private Context context;
    private IBinderPoor mIBinderPoor;
    private static volatile BinderPoor binderPoor;
    private CountDownLatch downLatch;

    public BinderPoor(Context context) {
    this.context = context.getApplicationContext();
    connectBinder();
    }

    public static BinderPoor getInsance(Context context) {
    if (binderPoor == null) {
    synchronized (BinderPoor.class) {
    if (binderPoor == null) {
    binderPoor = new BinderPoor(context);
    }
    }
    }
    return binderPoor;
    }

    private synchronized void connectBinder() {
    downLatch = new CountDownLatch(1);
    Intent intent = new Intent(context, BinderPoolService.class);
    context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    try {
    downLatch.await();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }

    ServiceConnection serviceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
    mIBinderPoor = IBinderPoor.Stub.asInterface(service);
    try {
    mIBinderPoor.asBinder().linkToDeath(mBindDeathRecipient, 0);
    downLatch.countDown();
    } catch (RemoteException e) {
    e.printStackTrace();
    }
    }

     @Override
     public void onServiceDisconnected(ComponentName name) {
    
     }
    

    };
    private IBinder.DeathRecipient mBindDeathRecipient = new IBinder.DeathRecipient() {
    @Override
    public void binderDied() {
    mIBinderPoor.asBinder().unlinkToDeath(mBindDeathRecipient, 0);
    connectBinder();
    }
    };

    public IBinder queryBinders(int biderCode) {
    IBinder binder = null;
    if (mIBinderPoor != null) {
    try {
    binder = mIBinderPoor.queryBindrt(biderCode);
    } catch (RemoteException e) {
    e.printStackTrace();
    }
    }
    return binder;
    }

    public static class BinderPoolIPer extends IBinderPoor.Stub {
    @Override
    public IBinder queryBindrt(int binderConde) throws RemoteException {
    IBinder binder = null;
    switch (binderConde) {
    case 1:
    binder = new Securitys();
    break;
    case 2:
    binder = new Computs();
    break;
    }
    return binder;
    }
    }
    }

/**

  • service
    */

public class BinderPoolService extends Service {
public Binder binder = new BinderPoor.BinderPoolIPer();
@Override
public void onCreate() {
super.onCreate();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return binder ;
}

@Override
public void onDestroy() {
    super.onDestroy();
}

}

你可能感兴趣的:(android在一个service 中绑定多个AIDL文件)