上一篇ArkUI eTS 计算十二生肖 是直接在eTS计算出生肖的,这篇文章是通过PA(Service Ability)来计算生肖的,UI是一样的,计算方式不同,通过此文章可以学习到eTS如何调用Java Ability,返回JSON字符串结果,显示到eTS上,由于预览器不支持调用PA, 所以这里使用的是远程模拟器P40 Pro 来开发测试.
首先创建Service Ability,如下图:
创建好Service Ability后,会自动在config.json生成以下配置:
下面开始来讲解Service Ability如何写并计算出生肖,如果之前没有JS 调用Java Ability经验的,可以先看一下官方文档CallAbility调用基本计算服务示例:
综合示例-JS FA调用Java PA机制-接口参考(JS及TS API)-手机、平板、智慧屏和智能穿戴开发-JS API参考-HarmonyOS应用开发
这个实例有详细讲解,我也是参考些文档.
FA在请求PA服务时会调用Ability.connectAbility连接PA,连接成功后,需要在onConnect返回一个remote对象,供FA向PA发送消息
@Override
protected IRemoteObject onConnect(Intent intent) {
super.onConnect(intent);
return remote.asObject();
}
class MyRemote extends RemoteObject implements IRemoteBroker {
private static final int SUCCESS = 0;
private static final int ERROR = 1;
private static final int PLUS = 1001;
/**
* 构造方法
*/
public MyRemote() {
super("MyService_MyRemote");
}
@Override
public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException {
// code是eTS传参messageCode
switch (code) {
case PLUS: {
// 读取参数数据
String dataStr = data.readString();
HiLog.info(LABEL_LOG, "xx" + dataStr);
Map<String, Object> param = new HashMap<>();
int year = -1;
try {
// 把读取到字符串参数转为Map对象
param = ZSONObject.stringToClass(dataStr, Map.class);
// 从对象中获取参数年份
year = Integer.parseInt( String.valueOf(param.get("year")));
HiLog.info(LABEL_LOG, "xx获取到年份是: " + year);
}catch (RuntimeException e) {
HiLog.error(LABEL_LOG, "convert failed.");
}
// Java计算十二生肖
String born = getBornFromJava(year);
// 返回结果当前仅支持String,对于复杂结构可以序列化为ZSON字符串上报
Map<String, Object> result = new HashMap<>();
result.put("code", SUCCESS);
result.put("abilityResult", born);
reply.writeString(ZSONObject.toZSONString(result));
break;
}
default: {
Map<String, Object> result = new HashMap<>();
result.put("code", ERROR);
result.put("abilityError", "服务器繁忙, 请稍后再试!!!");
reply.writeString(ZSONObject.toZSONString(result));
return false;
}
}
return true;
}
@Override
public IRemoteObject asObject() {
return this;
}
}
private String[] zodiac = {"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"};
/**
* Java计算十二生肖
* @param year
* @return
*/
private String getBornFromJava(int year) {
if (year == -1) { return "不是有效年份"; }
int idx = year % 12;
return zodiac[idx];
}
到此Java代码就讲解完了,下来看看eTS如何调用刚才创建的Service Ability。
// 计算生肖
getBorn() {
// eTS端计算十二生肖
// let idx = this.year%12
// this.born = this.zodiac[idx]
let that = this;
// 调用PA返回十二生肖
FeatureAbility.callAbility({
bundleName: "com.army.serverless",
abilityName: "com.army.serverless.ZodiacServiceAbility",
// abilityType: 0-Ability; 1-Internal Ability
abilityType: 0,
messageCode: 1001,
data: {year: this.year},
// syncOption(Optional, default sync): 0-Sync; 1-Async
syncOption: 0
}).then((data) => {
console.info("xx返回结果是: " + data);
let jsonObj = JSON.parse(data);
if(jsonObj.code === 0) {
that.born = jsonObj.abilityResult;
}else{
AlertDialog.show({
message: jsonObj.abilityError
})
}
})
}
计算生肖由Service Ability负责,eTS只负责UI,这样把业务逻辑抽出来了,比如Java UI也实现这个计算生肖功能,就可以调用Service Ability的计算生肖方法,从而把计算生肖方法共用出来,如果把计算生肖逻辑放到云函数上,那样H5, 小程序等其它都可以调用。