ArkUI eTS PA計(jì)算十二生肖--Service Ability
??想了解更多關(guān)于開源的內(nèi)容,請(qǐng)?jiān)L問:??
1、前言
這篇文章是通過PA(Service Ability)來計(jì)算生肖的,UI是一樣的,計(jì)算方式不同,通過此文章可以學(xué)習(xí)到eTS如何調(diào)用Java Ability,返回JSON字符串結(jié)果,顯示到eTS上,由于預(yù)覽器不支持調(diào)用PA, 所以這里使用的是遠(yuǎn)程模擬器P40 Pro 來開發(fā)測(cè)試。
2、效果
3、講解
首先創(chuàng)建Service Ability,如下圖:
創(chuàng)建好Service Ability后,會(huì)自動(dòng)在config.json生成以下配置:
下面開始來講解Service Ability如何寫并計(jì)算出生肖,如果之前沒有JS 調(diào)用Java Ability經(jīng)驗(yàn)的,可以先看一下官方文檔CallAbility調(diào)用基本計(jì)算服務(wù)示例 這個(gè)實(shí)例有詳細(xì)講解,我也是參考些文檔。
FA在請(qǐng)求PA服務(wù)時(shí)會(huì)調(diào)用Ability.connectAbility連接PA,連接成功后,需要在onConnect返回一個(gè)remote對(duì)象,供FA向PA發(fā)送消息。
protected IRemoteObject onConnect(Intent intent) {
super.onConnect(intent);
return remote.asObject();
}
Remote對(duì)象里面請(qǐng)求方法自己實(shí)現(xiàn),自定義MyRemote類。
class MyRemote extends RemoteObject implements IRemoteBroker {
private static final int SUCCESS = 0;
private static final int ERROR = 1;
private static final int PLUS = 1001;
/**
* 構(gòu)造方法
*/
public MyRemote() {
super("MyService_MyRemote");
}
public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException {
// code是eTS傳參messageCode
switch (code) {
case PLUS: {
// 讀取參數(shù)數(shù)據(jù)
String dataStr = data.readString();
HiLog.info(LABEL_LOG, "xx" + dataStr);
Map<String, Object> param = new HashMap<>();
int year = -1;
try {
// 把讀取到字符串參數(shù)轉(zhuǎn)為Map對(duì)象
param = ZSONObject.stringToClass(dataStr, Map.class);
// 從對(duì)象中獲取參數(shù)年份
year = Integer.parseInt( String.valueOf(param.get("year")));
HiLog.info(LABEL_LOG, "xx獲取到年份是: " + year);
}catch (RuntimeException e) {
HiLog.error(LABEL_LOG, "convert failed.");
}
// Java計(jì)算十二生肖
String born = getBornFromJava(year);
// 返回結(jié)果當(dāng)前僅支持String,對(duì)于復(fù)雜結(jié)構(gòu)可以序列化為ZSON字符串上報(bào)
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", "服務(wù)器繁忙, 請(qǐng)稍后再試!!!");
reply.writeString(ZSONObject.toZSONString(result));
return false;
}
}
return true;
}
public IRemoteObject asObject() {
return this;
}
}
根據(jù)年份計(jì)算生肖方法。
private String[] zodiac = {"猴", "雞", "狗", "豬", "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊"};
/**
* Java計(jì)算十二生肖
* @param year
* @return
*/
private String getBornFromJava(int year) {
if (year == -1) { return "不是有效年份"; }
int idx = year % 12;
return zodiac[idx];
}
到此Java代碼就講解完了,下來看看eTS如何調(diào)用剛才創(chuàng)建的Service Ability。
前端調(diào)用Service Ability,修改一下之前getBorn函數(shù)就可以。
// 計(jì)算生肖
getBorn() {
// eTS端計(jì)算十二生肖
// let idx = this.year%12
// this.born = this.zodiac[idx]
let that = this;
// 調(diào)用PA返回十二生肖
FeatureAbility.callAbility({
bundleName: "com.demo",
abilityName: "com.demo.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返回結(jié)果是: " + data);
let jsonObj = JSON.parse(data);
if(jsonObj.code === 0) {
that.born = jsonObj.abilityResult;
}else{
AlertDialog.show({
message: jsonObj.abilityError
})
}
})
}
4、總結(jié)
計(jì)算生肖由Service Ability負(fù)責(zé),eTS只負(fù)責(zé)UI,這樣把業(yè)務(wù)邏輯抽出來了,比如Java UI也實(shí)現(xiàn)這個(gè)計(jì)算生肖功能,就可以調(diào)用Service Ability的計(jì)算生肖方法,從而把計(jì)算生肖方法共用出來,如果把計(jì)算生肖邏輯放到云函數(shù)上,那樣H5, 小程序等其它都可以調(diào)用。