【问题形容】
文档形容了 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<String, Object> result = new HashMap<String, Object>();
result.put("code", SUCCESS);
result.put("abilityResult", "Pa data");
MessageParcel responseData = 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<String, Object> result = new HashMap<String, Object>();
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 配置文件
第三步 书写 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%')
}
}
运行成果:
更多精彩内容,请见华为开发者官方论坛→https://developer.huawei.com/…