-
Notifications
You must be signed in to change notification settings - Fork 20
自定义数据类型转换器TypeTransfer实现
Albie Liang edited this page Oct 27, 2019
·
1 revision
IPCInvoker目前默认支持了基础数据类型、List、Map和实现了Parcelable接口的数据类型,如果需要支持其它数据类型,需要提供该数据类型的BaseTypeTransfer
实现类,并在IPCInvoker初始化时将其添加ObjectTypeTransfer中。
需实现BaseTypeTransfer
接口,并在IPCInvoker初始化时将其添加到IPCInvoker的ObjectTypeTransfer中。
public class TestType {
public String key;
public String value;
}
public class TestTypeTransfer implements BaseTypeTransfer {
@Override
public boolean canTransfer(Object o) {
return o instanceof TestType;
}
@Override
public void writeToParcel(@NonNull Object o, Parcel dest) {
TestType testTypeObj = (TestType) o;
dest.writeString(testTypeObj.key);
dest.writeString(testTypeObj.value);
}
@Override
public Object readFromParcel(Parcel in) {
TestType testTypeObj = new TestType();
testTypeObj.key = in.readString();
testTypeObj.value = in.readString();
return testTypeObj;
}
}
public class IPCInvokerApplication extends Application {
private static final String TAG = "IPCInvokerSample.IPCInvokerApplication";
@Override
public void onCreate() {
super.onCreate();
// Initialize IPCInvoker
IPCInvokerBoot.setup(this, new DefaultInitDelegate() {
@Override
public void onAttachServiceInfo(IPCInvokerInitializer initializer) {
initializer.addIPCService(MainProcessIPCService.PROCESS_NAME, MainProcessIPCService.class);
initializer.addIPCService(PushProcessIPCService.PROCESS_NAME, PushProcessIPCService.class);
}
@Override
public void onAddTypeTransfer(TypeTransferInitializer initializer) {
super.onAddTypeTransfer(initializer);
initializer.addTypeTransfer(new TestTypeTransfer());
}
});
}
}
TestType data = new TestType();
data.key = "wx-developer";
data.value = "IPCInvoker";
final Integer result = IPCInvoker.invokeSync("cc.suitalk.ipcinvoker.sample:push", data, IPCInvokeTask_getInt.class);
Log.i(TAG, "result : %s", result.value);