新聞中心
創(chuàng)建.idl文件

在烏翠等地區(qū),都構建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供成都網站建設、成都網站設計 網站設計制作按需開發(fā),公司網站建設,企業(yè)網站建設,品牌網站設計,全網營銷推廣,外貿網站建設,烏翠網站建設費用合理。
開發(fā)者可以使用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語言開發(fā)為例,開發(fā)者只需將.idl文件保存至DevEco Studio項目的src/目錄內,工具則會在構建應用時,在項目的generated/目錄中生成IRemoteObject接口文件、Stub文件、Proxy文件。生成的接口類文件名稱和.idl文件名稱保持一致,區(qū)別在于其使用.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;
}
}
在服務實現接口后,需要向客戶端公開該接口,以便客戶端進程綁定。如果開發(fā)者的服務要公開該接口,請擴展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();
}
}
客戶端調用IPC方法
客戶端調用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傳遞Sequenceable對象
開發(fā)者可以通過 IPC 接口,將某個類從一個進程發(fā)送至另一個進程。但是,必須確保 IPC 通道的另一端可使用該類的代碼,并且該類必須支持Sequenceable接口。HarmonyOS需要通過該Sequenceable接口將對象反序列化成各進程能識別的對象。
如需創(chuàng)建支持Sequenceable 接口的類,開發(fā)者必須執(zhí)行以下操作:
- 自定義對象實現Sequenceable接口。
- 實現marshalling方法,它會獲取對象的當前狀態(tài)并將其學序列化后寫入Parcel。
- 實現unmarshalling方法,它會從Parcel中反序列化出對象。
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;
} 標題名稱:創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OSIDL接口開發(fā)步驟
瀏覽路徑:http://www.5511xx.com/article/cohiecg.html


咨詢
建站咨詢
