【HarmonyOS】【ArkUI】 鸿蒙 ets怎么调用PA

【问题描述】
【HarmonyOS】【ArkUI】 鸿蒙 ets怎么调用PA
文章图片

文档描述了js调用pa案例,没有ets调用pa用例?接下来就是ets怎么调用pa
参考文档链接
https://developer.harmonyos.c...
【解决方案】
第一步:新建pa的Ability(目前命名为MyServiceAbility)
参考资料链接
https://developer.harmonyos.c...
代码如下

// ohos相关接口包 public class ComputeInternalAbility extends AceInternalAbility { private static final String BUNDLE_NAME = "com.harmony.alliance.myets"; private static final String ABILITY_NAME = "com.harmony.alliance.myets.ComputeInternalAbility"; private static final int SUCCESS = 0; private static final int ERROR = 1; private static final int PLUS = 1001; // 定义日志标签 private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0, "MY_TAG"); private static ComputeInternalAbility instance; private AbilityContext abilityContext; // 如果多个Ability实例都需要注册当前InternalAbility实例,需要更改构造函数,设定自己的bundleName和abilityName public ComputeInternalAbility() { super(BUNDLE_NAME, ABILITY_NAME); } /** * Internal ability 注册接口。 */ public static void register(AbilityContext abilityContext) { instance = new ComputeInternalAbility(); instance.onRegister(abilityContext); } /** * Internal ability 注销接口。 */ public static void unregister() { instance.onUnregister(); } public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) { switch (code) { case PLUS: { System.err.println("app log pa得到ets数据"+data.readString()); Map result = new HashMap(); result.put("code", SUCCESS); result.put("abilityResult", "Pa data"); MessageParcel responseData = https://www.it610.com/article/MessageParcel.obtain(); responseData.writeString(ZSONObject.toZSONString(result)); IRemoteObject remoteReply = reply.readRemoteObject(); try { remoteReply.sendRequest(0, responseData, MessageParcel.obtain(), new MessageOption()); } catch (RemoteException exception) { return false; } finally { responseData.reclaim(); } break; } default: { Map result = new HashMap(); result.put("abilityError", ERROR); reply.writeString(ZSONObject.toZSONString(result)); return false; } } return true; } private void onRegister(AbilityContext abilityContext) { this.abilityContext = abilityContext; this.setInternalAbilityHandler((code, data, reply, option) -> { return this.onRemoteRequest(code, data, reply, option); }); } private void onUnregister() { abilityContext = null; this.setInternalAbilityHandler(null); } }

第二步 查看config.json是否abilities是否该Pa配置文件
【HarmonyOS】【ArkUI】 鸿蒙 ets怎么调用PA
文章图片

第三步 书写MainAbility的onStart()和onStop()代码 代码如下
package com.harmony.alliance.myets; import ohos.ace.ability.AceAbility; import ohos.aafwk.content.Intent; import ohos.security.SystemPermission; public class MainAbility extends AceAbility { @Override public void onStart(Intent intent) { ComputeInternalAbility.register(this); super.onStart(intent); } @Override public void onStop() { ComputeInternalAbility.unregister(); super.onStop(); SampleJSInterface.getInstance().deregister(this); } }

第四步 书写ets文件
@Entry @Component struct MyService { build() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { Text('ets调用pa') .fontSize(50) .onClick(function () { FeatureAbility.callAbility({ bundleName: 'com.harmony.alliance.myets', abilityName: 'com.harmony.alliance.myets.ComputeInternalAbility', messageCode: 1001, abilityType: 1, data: { firstNum: 1, secondNum: 2 }, syncOption: 0 }).then((data) => { console.log('pa传递给ets的数据 ' + data); }) }) .fontWeight(FontWeight.Bold) } .width('100%') .height('100%') } }

运行效果:
【HarmonyOS】【ArkUI】 鸿蒙 ets怎么调用PA
文章图片

【【HarmonyOS】【ArkUI】 鸿蒙 ets怎么调用PA】更多精彩内容,请见华为开发者官方论坛→https://developer.huawei.com/...

    推荐阅读