鸿蒙系统-手机-JS FA(Feature Ability)调用Java PA(Particle Ability)
1.编写PA代码
PlayAbility.c
package com.example.phone.ability;
import com.example.phone.param.SUMRequestParam;
import com.example.phone.utils.PlayMusicUtil;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.*;
import ohos.utils.zson.ZSONObject;
import java.util.HashMap;
import java.util.Map;
public class PlayAbility extends Ability {
private static final String TAG = "PlayAbility";
static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 1, "MY_TAG");
private static final int ERROR = -1;
private static final int SUCCESS = 0;
private static final int PLUS = 1001;
private PlayRemote remote;
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
}
@Override
protected IRemoteObject onConnect(Intent intent) {
super.onConnect(intent);
Context c = getContext();
remote = new PlayRemote();
return remote.asObject();
}
class PlayRemote extends RemoteObject implements IRemoteBroker {
public PlayRemote() {
super("PlayRemote 666");
}
@Override
public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException {
switch (code) {
case PLUS: {
String zsonStr = data.readString();
SUMRequestParam param = ZSONObject.stringToClass(zsonStr, SUMRequestParam.class);
// 返回结果仅支持可序列化的Object类型
Map<String, Object> zsonResult = new HashMap<String, Object>();
zsonResult.put("code", SUCCESS);
zsonResult.put("abilityResult", param.getFirstNum() + param.getSecondNum());
reply.writeString(ZSONObject.toZSONString(zsonResult));
break;
}
default: {
reply.writeString("service not defined");
return false;
}
}
return true;
}
@Override
public IRemoteObject asObject() {
return this;
}
}
}
2.编写FA
index.js
// 下面的代码编译器报错不用管
const globalRef = Object.getPrototypeOf(global) || global
globalRef.regeneratorRuntime = require('@babel/runtime/regenerator')
const ABILITY_TYPE_EXTERNAL = 0;
const ACTION_SYNC = 0;
const ACTION_ASYNC = 1;
const ACTION_MESSAGE_CODE_PLUS = 1001;
export const playAbility = {
sum: async function(that){
var actionData = {};
actionData.firstNum = 1024;
actionData.secondNum = 2048;
var action = {};
action.bundleName = 'com.example.phone';
action.abilityName = 'com.example.phone.ability.PlayAbility';
action.messageCode = ACTION_MESSAGE_CODE_PLUS;
action.data = actionData;
action.abilityType = ABILITY_TYPE_EXTERNAL;
action.syncOption = ACTION_SYNC;
var result = await FeatureAbility.callAbility(action);
var ret = JSON.parse(result);
if (ret.code == 0) {
console.info('plus result is:' + JSON.stringify(ret.abilityResult));
that.title = 'plus result is:' + JSON.stringify(ret.abilityResult);
} else {
console.error('plus error code:' + JSON.stringify(ret.code));
}
}
}
export default {
data: {
title: ""
},
onInit() {
this.title = "我是大喵666";
},
play(){
this.title = "我是大喵计算中";
playAbility.sum(this);
}
}
index.hml
<div class="container">
<div>
<text class="title">
{
{ $t('strings.hello') }}{
{title}}
</text>
</div>
<div>
<text class="title" onclick="play">
计算
</text>
</div>
</div>