HarmonyOS IDL(HarmonyOS Interface Definition Language)
客户端和服务端定义双方都认可的通信接口
- 面向应用程序的北向接口
- 面向硬件设备的南向接口
IPC/RPC通信模型
模型说明:
- 服务端提供服务接口,通过继承编译生成的桩(Stub)类。
- 客户端调用编译时生成的代理类(Proxy)
使用IDL相互通信的优势:
- 以接口的形式定义服务,隐藏实现细节
- 支持跨设备/跨进程调用
基本数据类型
HarmonyOS IDL基本数据类型
Java数据类型
数据长度(bytes)
void
void
NA
boolean
boolean
1
byte
byte
1
short
short
2
int
int
4
long
long
8
float
float
4
double
double
8
String
String
NA
Sequenceable数据类型
Sequenceable数据类型是指用“Sequenceable”关键字声明的非基本数据类型,表明该数据类型可以通过Parcel进行跨进程(或跨设备)传递。
HarmonyOS IDL数据类型
Java数据类型
Sequenceable
sequenceable namespace.typename;
例如:sequenceable com.huawei.samples.ApplicationInfo;
数组类型
HarmonyOS IDL数据类型
Java数据类型
T[]
T[]
容器类型
HarmonyOS IDL数据类型
Java数据类型
List
List
Map
HashMap
服务端实现步骤
- 自定义对象实现Sequenceable接口
package ohos.samples.rpcserver.model; import ohos.utils.Parcel; import ohos.utils.Sequenceable; public class SequenceableData implements Sequenceable { private int numA = 0; private int numB = 0; public SequenceableData() { } public SequenceableData(int numA, int numB) { this.numA = numA; this.numB = numB; } @Override public boolean marshalling(Parcel out) { // 将数据写入到out中 return out.writeInt(numA) && out.writeInt(numB); } @Override public boolean unmarshalling(Parcel in) { // 根据marshalling方法中写入的顺序从in中读取数据 this.numA = in.readInt(); this.numB = in.readInt(); return true; } public int getNumA() { return numA; } public int getNumB() { return numB; } }
- 定义IDL文件,然后gradle中执行compileDebugIdl编译idl文件
sequenceable ohos.samples.rpcserver.model.SequenceableData; interface ohos.samples.rpcserver.IMyIdlInterface { int addNumber([in] SequenceableData sequenceableData); }
- 服务端实现idl接口
package ohos.samples.rpcserver; import ohos.aafwk.ability.Ability; import ohos.aafwk.content.Intent; import ohos.rpc.IRemoteObject; import ohos.rpc.RemoteException; import ohos.samples.rpcserver.model.SequenceableData; public class ServiceAbility extends Ability { @Override protected IRemoteObject onConnect(Intent intent) { return new IMyIdlInterfaceStubImpl("starting IMyIdlInterface"); } private static class IMyIdlInterfaceStubImpl extends MyIdlInterfaceStub { public IMyIdlInterfaceStubImpl(String descriptor) { super(descriptor); } @Override public int addNumber(SequenceableData sequenceableData) throws RemoteException { LogUtil.info("","numA+numB=" + (sequenceableData.getNumA() + sequenceableData.getNumB())); return sequenceableData.getNumA() + sequenceableData.getNumB(); } } }
客户端实现步骤
- 复制Server端实现Sequenceable接口的对象到客户端项目。
注意:包名和类名需要跟服务端的对象保持一致。 - 复制ServerIDL接口文件到客户端项目中,然后gradle中执行compileDebugIdl。 |
注意:接口的包名和idl文件名需要跟服务端idl文件保持一致。 - 客户端连接服务端
private MyIdlInterfaceProxy myIdlInterfaceProxy; private void startServer() { Intent intent = new Intent(); ElementName elementName = new ElementName("", "ohos.samples.rpcserver", "ohos.samples.rpcserver.ServiceAbility"); intent.setElement(elementName); connectAbility(intent, connection); } private final IAbilityConnection connection = new IAbilityConnection() { @Override public void onAbilityConnectDone(ElementName elementName, IRemoteObject remoteObject, int resultCode) { myIdlInterfaceProxy = new MyIdlInterfaceProxy(remoteObject); } @Override public void onAbilityDisconnectDone(ElementName elementName, int resultCode) { } };
- 客户端调用服务端IDL接口
private void addNumbers() { try { int sum = myIdlInterfaceProxy.addNumber(new SequenceableData(firstNumber,secondNumber)); } catch (RemoteException | NumberFormatException e) { LogUtil.error(TAG, "Remote Exception or Number Format Exception"); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)