项目作者: xyxyLiu

项目描述 :
Android IPC framework
高级语言: Java
项目地址: git://github.com/xyxyLiu/AndInvoker.git
创建时间: 2019-11-12T06:16:36Z
项目社区:https://github.com/xyxyLiu/AndInvoker

开源协议:Apache License 2.0

下载


AndInvoker

A tiny Android IPC framework

based on Android Binder, ContentProvider.

Download

Gradle Dependency

  1. repositories {
  2. ...
  3. jcenter()
  4. }
  5. dependencies {
  6. implementation 'com.reginald:andinvoker:xxx'
  7. }
  8. `

Usage

Register ContentProviders

define AndInvokerProviders for each ipc process

  1. public class ProcessAProvider extends AndInvokerProvider {}
  2. public class ProcessBProvider extends AndInvokerProvider {}
  3. ....
  4. `

register them in AndroidManifest.xml

  1. <provider
  2. android:name="ProcessAProvider"
  3. android:authorities="${applicationId}.process.a"
  4. android:exported="false"
  5. android:process=":a" ></provider>
  6. <provider
  7. android:name="ProcessBProvider"
  8. android:authorities="${applicationId}.process.b"
  9. android:exported="false"
  10. android:process=":b" ></provider>
  11. ....
  12. `

Register service (Binder/IInvoker/Interface)

  • Register a Binder
    ```java
    // your aidl Binder
    public class MyBinder extends IMyBinder.Stub {
    …….
    }

// register a binder service with name in local/remote process
AndInvoker.registerService(context, “provider_authorities”, “binder_name”, new IServiceFetcher() {
@Override
public IBinder onFetchService(Context context) {
return new MyBinder();
}
});

  1. * Register an Invoker
  2. ```java
  3. public class MyInvoker implements IInvoker {
  4. @Override
  5. public Bundle onInvoke(Context context, String methodName, Bundle params, ICall callback) {
  6. // handle invoke here ...
  7. // callback here if needed
  8. if (callback != null) {
  9. Bundle data = new Bundle();
  10. // ...
  11. callback.onCall(data);
  12. }
  13. // return result
  14. return new Bundle();
  15. }
  16. }
  17. // register invoker in local/remote process
  18. AndInvoker.registerInvoker(context, "provider_authorities", "invoker_name", MyInvoker.class);
  • Register an interface
    interface shared between processes must be annotated with @RemoteInterface.
  1. @RemoteInterface
  2. public interface IMyInterface {
  3. String testBasicTypes(int i, long l, String s);
  4. Bundle setCallback(@RemoteInterface IMyCallback callback);
  5. }
  6. // register interface in local/remote process
  7. AndInvoker.registerInterface(context, "provider_authorities", "interface_name", new IMyInterfaceImpl(), IMyInterface.class);
  8. `

register data codec for your custom data type(demo: GsonCodec)

  1. AndInvoker.appendCodec(Class<S> yourCustomClass, Class<R> serializeClass, Codec<S, R> codec);
  2. `

Fetch service (Binder/IInvoker/Interface)

  • Fetch a Binder
    ```java
    IBinder binderService = AndInvoker.fetchServiceNoThrow(context,
    1. "provider_authorities", "binder_name");

IMyBinder myBinder = IMyBinder.Stub.asInterface(binderService);

  1. * Invoke an IInvoker
  2. ```java
  3. Bundle result = AndInvoker.invokeNoThrow(context, "provider_authorities", "invoker_name","method_name", params, callback)
  • Fetch an interface
    ```java
    IMyInterface myInterface = AndInvoker.fetchInterfaceNoThrow(context, “provider_authorities”, “interface_name”, IMyInterface.class);

try {
myInterface.testBasicTypes(1, 2L, “test”);
} catch(InvokeException e) {
// may throw InvokeException if remote service dies or other remote errors
}

````