A more convenient solution for cross-process communication in Android.No need to use aidl.
Chinese document
Add dependency
- Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
......
maven { url 'https://jitpack.io' }
}
}Copy the code
- Add the dependency:
Dependencies {implementation 'com. Making. Zhangke3016: CmProcess: 1.0.1'}Copy the code
Use
- Init in your application:
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
VCore.init(base);
}
Copy the code
- 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);
}
Copy the code
- 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);
Copy the code
- 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 } }); }
Copy the code
- 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");Copy the code