日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OSIDL接口開發(fā)步驟

創(chuàng)建.idl文件

在烏翠等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計 網(wǎng)站設(shè)計制作按需開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計,全網(wǎng)營銷推廣,外貿(mào)網(wǎng)站建設(shè),烏翠網(wǎng)站建設(shè)費用合理。

開發(fā)者可以使用Java或C++編程語言構(gòu)建.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/目錄內(nèi),工具則會在構(gòu)建應(yīng)用時,在項目的generated/目錄中生成IRemoteObject接口文件、Stub文件、Proxy文件。生成的接口類文件名稱和.idl文件名稱保持一致,區(qū)別在于其使用.java 擴展名。例如,IRemoteAbility.idl 生成的文件名是 IRemoteAbility.java、RemoteAbilityProxy.java、RemoteAbilityStub.java。

服務(wù)端公開接口

HarmonyOS IDL工具生成的Stub類是接口類的抽象實現(xiàn),并且會聲明.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;
    ………
}

要實現(xiàn).idl生成的接口,請擴展生成的RemoteObject接口,并實現(xiàn)繼承自.idl文件的方法。MyRemote定義了服務(wù)的遠(yuǎn)程過程調(diào)用接口,示例代碼如下:

class MyRemote extends MyRemoteAbilityStub {

 
    public MyRemote(String descriptor) {
        super(descriptor);
    }

 
    @Override
    public int plus(int num1, int num2) throws RemoteException {
        return num1 + num2;
    }
}

在服務(wù)實現(xiàn)接口后,需要向客戶端公開該接口,以便客戶端進程綁定。如果開發(fā)者的服務(wù)要公開該接口,請擴展Ability并實現(xiàn)onConnect()從而返回IRemoteObject,以便客戶端能與服務(wù)進程交互。服務(wù)端向客戶端公開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();
    }
}

客戶端調(diào)用IPC方法

客戶端調(diào)用connectAbility()以連接服務(wù)時,客戶端的onAbilityConnectDone()回調(diào)會接收服務(wù)的onConnect()方法返回的IRemoteObject實例。由于客戶端和服務(wù)在不同應(yīng)用內(nèi),所以客戶端應(yīng)用的src/目錄內(nèi)必須包含.idl文件(SDK工具會自動生成Proxy代理類)的副本。當(dāng)客戶端onAbilityConnectDone()回調(diào)中收到IRemoteObject,調(diào)用RemoteAbilityStub.asInterface(IRemoteObject),以將返回的參數(shù)轉(zhuǎn)換為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;
    }
};

如果要斷開連接,請調(diào)用Ability.disconnectAbility()。

IPC傳遞Sequenceable對象

開發(fā)者可以通過 IPC 接口,將某個類從一個進程發(fā)送至另一個進程。但是,必須確保 IPC 通道的另一端可使用該類的代碼,并且該類必須支持Sequenceable接口。HarmonyOS需要通過該Sequenceable接口將對象反序列化成各進程能識別的對象。

如需創(chuàng)建支持Sequenceable 接口的類,開發(fā)者必須執(zhí)行以下操作:

  1. 自定義對象實現(xiàn)Sequenceable接口。
  2. 實現(xiàn)marshalling方法,它會獲取對象的當(dāng)前狀態(tài)并將其學(xué)序列化后寫入Parcel。
  3. 實現(xiàn)unmarshalling方法,它會從Parcel中反序列化出對象。

ElementName.java類實現(xiàn)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;
    }

網(wǎng)頁名稱:創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OSIDL接口開發(fā)步驟
文章分享:http://m.5511xx.com/article/cohiecg.html