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

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值