IPCInvoker

Introduction: A IPC Invoker for Android Development.
More: Author   ReportBugs   
Tags:

license Release Version wiki PRs Welcome

在 Android 开发过程中,经常需要写一些跨进程的逻辑,一般情况下我们都是通过 AIDL 接口来调用的,写 AIDL 接口并不是一件容易的事情,需要写 Service,定义特定的业务接口,调用时需要绑定 Service 等。

IPCInvoker 就是一个用来简化跨进程调用的组件,IPCInvoker 底层也是通过 AIDL 实现的,只是把接口封装得更加容易使用。

引入组件库

IPCInvoker 组件库已经提交到 jcenter 上了,可以直接 dependencies 中配置引用

dependencies {
    api 'cc.suitalk.android:ipc-invoker:<last-version>'
}

在项目中使用

为每个需要支持 IPCInvoker 的进程创建一个 Service

这里以 PushProcessIPCService 为示例,代码如下:


public class PushProcessIPCService extends BaseIPCService {

    public static final String PROCESS_NAME = "cc.suitalk.ipcinvoker.sample:push";

    @Override
    public String getProcessName() {
        return PROCESS_NAME;
    }
}

在 manifest.xml 中配置 service

<service android:name="cc.suitalk.ipcinvoker.sample.service.PushProcessIPCService" android:process=":push"/>

在项目的 Application 中 setup IPCInvoker

这里需要在你的项目所有需要支持跨进程调用的进程中调用IPCInvoker.setup(Application, IPCInvokerInitDelegate)方法,并在传入的 IPCInvokerInitDelegate 接口实现中将该进程需要支持访问的其它进程相应的 Service 的 class 添加到 IPCInvoker 当中,示例如下:

public class IPCInvokerApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        // Initialize IPCInvoker
        IPCInvoker.setup(this, new DefaultInitDelegate() {

            @Override
            public void onAttachServiceInfo(IPCInvokerInitializer initializer) {
                initializer.addIPCService(PushProcessIPCService.PROCESS_NAME, PushProcessIPCService.class);
            }
        });
    }
}

在项目代码中通过 IPCInvoker 调用跨进程逻辑

通过 IPCInvoker 同步调用跨进程逻辑

public class InvokeSyncSample {

    private static final String TAG = "InvokeSyncSample";

    public static void invokeSync() {
        Integer result = IPCInvoker.invokeSync(
                PushProcessIPCService.PROCESS_NAME, "Albie", HashCode.class);
        Log.i(TAG, "invoke result : %s", result);
    }

    private static class HashCode implements IPCSyncInvokeTask<String, Integer> {

        @Override
        public Integer invoke(String data) {
            return data.hashCode();
        }
    }
}

通过 IPCInvoker 异步调用跨进程逻辑

public class InvokeAsyncSample {

    private static final String TAG = "InvokeAsyncSample";

    public static void invokeAsync() {
        IPCInvoker.invokeAsync(PushProcessIPCService.PROCESS_NAME,
                "Albie", HashCode.class, new IPCInvokeCallback<Integer>() {
            @Override
            public void onCallback(Integer data) {
                Log.i(TAG, "onCallback : hascode : %d", data);
            }
        });
    }

    private static class HashCode implements IPCAsyncInvokeTask<String, Integer> {

        @Override
        public void invoke(String data, IPCInvokeCallback<Integer> callback) {
            callback.onCallback(data.hashCode());
        }
    }
}

IPCInvoker 支持自定义实现的 Parcelable 类作为跨进程调用的数据结构,同时也支持基础类型的包装类、Map 和 List,对于既非基础类型也非 Parcelable 类型,则需要自定义 TypeTransfer来达到可跨进程传输的效果。

通过 IPCTask 实现跨进程调用

IPCTask 提供了相对 IPCInvoker 更为丰富的接口,支持设置连接超时,连接回调,异常回调和确实结果等。与此同时 IPCTask 支持基本数据类型,并提供了不同于 IPCInvoker 链式调用方式。

异步调用

public class IPCTaskTestCase {

    private static final String TAG = "IPCTaskTestCase";

    public static void invokeAsync() {
        IPCTask.create("cc.suitalk.ipcinvoker.sample:push")
                .timeout(10)
                .async(AsyncInvokeTask.class)
                .data("test invokeAsync")
                .defaultResult(false)
                .callback(true, new IPCInvokeCallback<Boolean>() {
                    @Override
                    public void onCallback(Boolean data) {
                        /// callback on UI Thread
                        Log.i(TAG, "invokeAsync result : %s", data);
                    }
                }).invoke();
    }

    private static class AsyncInvokeTask implements IPCAsyncInvokeTask<String, Boolean> {

        @Override
        public void invoke(String data, IPCInvokeCallback<Boolean> callback) {
            callback.onCallback(true);
        }
    }
}

同步调用

public class IPCTaskTestCase {

    private static final String TAG = "IPCTaskTestCase";

    public static void invokeSync() {
        Map<String, Object> map = new HashMap<>();
        map.put("key", "value");
        Boolean result = IPCTask.create("cc.suitalk.ipcinvoker.sample:push")
                .timeout(20)
                .sync(SyncInvokeTask.class)
                .data(map)
                .defaultResult(false)
                .invoke();
        Log.i(TAG, "invokeSync result : %s", result);
    }

    private static class SyncInvokeTask implements IPCSyncInvokeTask<Map<String, Object>, Boolean> {

        @Override
        public Boolean invoke(Map<String, Object> data) {
            return true;
        }
    }
}

此外,IPCInvoker 还支持跨进程事件监听和分发等丰富的功能,详细使用说明请参考wiki ,更多使用示例请移步Sample 工程

License

   Copyright 2017 Albie Liang

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools