一行代码实现Android的跨进程调用与通信

阅读 490
收藏 13
2019-02-16
原文链接:github.com

A more convenient solution for cross-process communication in Android.No need to use aidl.

中文文档

Add dependency

  1. Add it in your root build.gradle at the end of repositories:
    allprojects {
                repositories {
                    ......
                    maven { url 'https://jitpack.io' }
        }
    }
  1. Add the dependency:
    dependencies {
        implementation 'com.github.zhangke3016:CmProcess:1.0.1'
    }

Use

  1. Init in your application:
  @Override
  protected void attachBaseContext(Context base) {
      super.attachBaseContext(base);
      VCore.init(base);
  }
  1. Define interfaces and implement ,the interface parameter type must be a primitive data type or a serializable/Parcelable type.eg:
  public interface IPayManager {
     String pay(int count);
     //if use remote service, the callback interface must be the provided 'IPCCallback`
     String pay(int count, IPCCallback callBack);
  }
  1. Register/Unregister your service at any time, anywhere
  //register local + remote service
  VCore.getCore().registerService(IPayManager.class, this);
  //unregister local + remote service
  VCore.getCore().unregisterService(IPayManager.class);
  //register local service
  VCore.getCore().registerLocalService(IPayManager.class, this);
  //unregister local service
  VCore.getCore().unregisterLocalService(IPayManager.class);
  1. Get services at any time, anywhere, any process
  IPayManager service = VCore.getCore().getService(IPayManager.class);
  //get local service
  //IPayManager service = VCore.getCore().getLocalService(IPayManager.class);
  //note service may be null if no service found
  if(service != null){
    //Synchronous call.
    String message = service.pay(5000);
    //Asynchronous call. note: use remote service, the callback interface must be the provided 'IPCCallback`
    service.pay(5000, new BaseCallback() {
         @Override
         public void onSucceed(Bundle result) {
             //Main thread
         }
    
         @Override
         public void onFailed(String reason) {
             //Main thread
         }
     });
  }
  1. Event subscription and post
    VCore.getCore().subscribe("key", new EventCallback() {
        @Override
        public void onEventCallBack(Bundle event) {
            //main thread
            String name = event.getString("name");
            Log.e("TAG", "onEventCallBack: " + name + " " + (Looper.myLooper() == Looper.getMainLooper()));
        }
    });
    
    //post:
    Bundle bundle = new Bundle();
    bundle.putString("name", "DoDo");
    VCore.getCore().post("key",bundle);
    
    //unsubscribe 
    VCore.getCore().unsubscribe("key");
评论