开发者可以使用Java或C++编程语言构建.idl文件。.idl示例如下:
package com.example
/** Example service ability interface */
interface com.example.IRemoteAbility {
int plus([in] int num1, [in] int num2);
}
HarmonyOS SDK工具支持生成Java、C++语言的接口类、桩类和代理类。以Java语言开发为例,开发者只需将.idl文件保存至DevEco Studio项目的src/目录内,工具则会在构建应用时,在项目的generated/目录中生成IRemoteObject接口文件、Stub文件、Proxy文件。生成的接口类文件名称和.idl文件名称保持一致,区别在于其使用.java 扩展名。例如,IRemoteAbility.idl 生成的文件名是 IRemoteAbility.java、RemoteAbilityProxy.java、RemoteAbilityStub.java。
HarmonyOS IDL工具生成的Stub类是接口类的抽象实现,并且会声明.idl文件中的所有方法。
public abstract class RemoteAbilityStub extends RemoteObject implements IRemoteAbility {
private static final String DESCRIPTOR = "com.example.IRemoteAbility";
private static final int COMMAND_PLUS = IRemoteObject.MIN_TRANSACTION_ID + 0;
………
}
要实现.idl生成的接口,请扩展生成的RemoteObject接口,并实现继承自.idl文件的方法。MyRemote定义了服务的远程过程调用接口,示例代码如下:
class MyRemote extends MyRemoteAbilityStub {
public MyRemote(String descriptor) {
super(descriptor);
}
@Override
public int plus(int num1, int num2) throws RemoteException {
return num1 + num2;
}
}
在服务实现接口后,需要向客户端公开该接口,以便客户端进程绑定。如果开发者的服务要公开该接口,请扩展Ability并实现onConnect()从而返回IRemoteObject,以便客户端能与服务进程交互。服务端向客户端公开IRemoteAbility接口的代码示例如下:
public class ServiceAbility extends Ability {
private MyRemote remote = new MyRemote("MyRemoteAbility");
class MyRemote extends RemoteAbilityStub {
public MyRemote(String descriptor) {
super(descriptor);
}
@Override
public int plus(int num1, int num2) throws RemoteException {
return num1 + num2;
}
}
@Override
public IRemoteObject onConnect(Intent intent) {
return remote.asObject();
}
}
客户端调用connectAbility()以连接服务时,客户端的onAbilityConnectDone()回调会接收服务的onConnect()方法返回的IRemoteObject实例。由于客户端和服务在不同应用内,所以客户端应用的src/目录内必须包含.idl文件(SDK工具会自动生成Proxy代理类)的副本。当客户端onAbilityConnectDone()回调中收到IRemoteObject,调用RemoteAbilityStub.asInterface(IRemoteObject),以将返回的参数转换为IRemoteAbility类型,例如:
IRemoteAbility proxy;
private IAbilityConnection conn = new IAbilityConnection() {
@Override
public void onAbilityConnectDone(ElementName element, IRemoteObject remote, int resultCode) {
proxy = IRemoteAbilityStub.asInterface(remote);
}
@Override
public void onAbilityDisconnectDone(ElementName element, int resultCode) {
proxy = null;
}
};
如果要断开连接,请调用Ability.disconnectAbility()。
开发者可以通过 IPC 接口,将某个类从一个进程发送至另一个进程。但是,必须确保 IPC 通道的另一端可使用该类的代码,并且该类必须支持Sequenceable接口。HarmonyOS需要通过该Sequenceable接口将对象反序列化成各进程能识别的对象。
如需创建支持Sequenceable 接口的类,开发者必须执行以下操作:
ElementName.java类实现Sequenceable接口的代码示例如下:
public class ElementName implements Sequenceable {
private String deviceId = "";
private String bundleName = "";
private String abilityName = "";
public ElementName() {
}
public ElementName(String deviceId, String bundleName, String abilityName) {
this.deviceId = deviceId;
this.bundleName = bundleName;
this.abilityName = abilityName;
}
/**
* @see ohos.utils.Sequenceable#marshalling(Parcel)
*
*/
@Override
public boolean marshalling(Parcel out) {
if (!out.writeString(bundleName)) {
return false;
}
if (!out.writeString(abilityName)) {
return false;
}
if (!out.writeString(deviceId)) {
return false;
}
return true;
}
/**
* @see ohos.utils.Sequenceable#unmarshalling(Parcel)
*
*/
@Override
public boolean unmarshalling(Parcel in) {
this.bundleName = in.readString();
this.abilityName = in.readString();
this.deviceId = in.readString();
return true;
}
备案信息: 粤ICP备15087711号-2
Copyright © 2008-2024 啊嘎哇在线工具箱 All Rights.
本站所有资料来源于网络,版权归原作者所有,仅作学习交流使用,如不慎侵犯了您的权利,请联系我们。