使用兼容JS的类Web开发范式的方舟开发框架提供了JS FA(Feature Ability)调用Java PA(Particle Ability)的机制,该机制提供了一种通道来传递方法调用、处理数据返回以及订阅事件上。
当前提供Ability和Internal Ability两种调用方式。
对于Internal Ability调用方式的开发,可以使用工具自动生成代码(自动生成下篇文章详解),提高开发效率。
JS端与Java端通过bundleName和abilityName来进行关联。在系统收到JS调用请求后,根据开发者在JS接口中设置的参数来选择对应的处理方式。开发者在onRemoteRequest()中实现PA提供的业务逻辑。
FA端提供以下三个JS接口:
PA端提供以下两类接口:
返回该错误说明JS接口调用请求未在系统中找到对应的InternalAbilityHandler进行处理,因此需要检查以下几点是否正确执行:
Ability和Internal Ability是两种不同的FA调用PA的方式。
Ability和InternalAbility差异项
差异项 |
Ability |
InternalAbility |
---|---|---|
JS端(abilityType) |
0 |
1 |
是否需要在config.json的abilities中为PA添加声明 |
需要(有独立的生命周期) |
不需要(和FA共生命周期) |
是否需要在FA中注册 |
不需要 |
需要 |
继承的类 |
ohos.aafwk.ability.Ability |
ohos.ace.ability.AceInternalAbility |
是否允许被其他FA访问调用 |
是 |
否 |
JS端调用FeatureAbility接口,传入两个Number参数,Java端接收后返回两个数的和。
JS FA应用的JS模块(entry/src/main)的典型开发目录结构如下:
使用Internal Ability方式时,需要将对应的action.abilityType值改为ABILITY_TYPE_INTERNAL。
// abilityType: 0-Ability; 1-Internal Ability
const ABILITY_TYPE_EXTERNAL = 0;
const ABILITY_TYPE_INTERNAL = 1;
// syncOption(Optional, default sync): 0-Sync; 1-Async
const ACTION_SYNC = 0;
const ACTION_ASYNC = 1;
const ACTION_MESSAGE_CODE_PLUS = 1001;
export default {
plus: async function() {
var actionData = {};
actionData.firstNum = 1024;
actionData.secondNum = 2048;
var action = {};
action.bundleName = 'com.example.hiaceservice';
action.abilityName = 'com.example.hiaceservice.ComputeServiceAbility';
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));
} else {
console.error('plus error code:' + JSON.stringify(ret.code));
}
}
}
功能代码实现:
在java目录下新建一个Service Ability,文件命名为ComputeServiceAbility.java
package com.example.hiaceservice;
// ohos相关接口包
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.IRemoteBroker;
import ohos.rpc.IRemoteObject;
import ohos.rpc.RemoteObject;
import ohos.rpc.MessageParcel;
import ohos.rpc.MessageOption;
import ohos.utils.zson.ZSONObject;
import java.util.HashMap;
import java.util.Map;
public class ComputeServiceAbility extends Ability {
// 定义日志标签
private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0, "MY_TAG");
private MyRemote remote = new MyRemote();
// 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;
MyRemote() {
super("MyService_MyRemote");
}
@Override
public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) {
switch (code) {
case PLUS: {
String dataStr = data.readString();
RequestParam param = new RequestParam();
try {
param = ZSONObject.stringToClass(dataStr, RequestParam.class);
} catch (RuntimeException e) {
HiLog.error(LABEL, "convert failed.");
}
// 返回结果当前仅支持String,对于复杂结构可以序列化为ZSON字符串上报
Map result = new HashMap();
result.put("code", SUCCESS);
result.put("abilityResult", param.getFirstNum() + param.getSecondNum());
reply.writeString(ZSONObject.toZSONString(result));
break;
}
default: {
Map result = new HashMap();
result.put("abilityError", ERROR);
reply.writeString(ZSONObject.toZSONString(result));
return false;
}
}
return true;
}
@Override
public IRemoteObject asObject() {
return this;
}
}
}
请求参数代码:
RequestParam.java
public class RequestParam {
private int firstNum;
private int secondNum;
public int getFirstNum() {
return firstNum;
}
public void setFirstNum(int firstNum) {
this.firstNum = firstNum;
}
public int getSecondNum() {
return secondNum;
}
public void setSecondNum(int secondNum) {
this.secondNum = secondNum;
}
}
PA端(Internal Ability方式)
功能代码实现(以下代码可以使用工具自动生成):
在java目录下新建一个Service Ability,文件命名为ComputeInternalAbility.java
package com.example.hiaceservice;
// ohos相关接口包
import ohos.ace.ability.AceInternalAbility;
import ohos.app.AbilityContext;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.IRemoteObject;
import ohos.rpc.MessageOption;
import ohos.rpc.MessageParcel;
import ohos.rpc.RemoteException;
import ohos.utils.zson.ZSONObject;
import java.util.HashMap;
import java.util.Map;
public class ComputeInternalAbility extends AceInternalAbility {
private static final String BUNDLE_NAME = "com.example.hiaceservice";
private static final String ABILITY_NAME = "com.example.hiaceservice.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);
}
public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) {
switch (code) {
case PLUS: {
String dataStr = data.readString();
RequestParam param = new RequestParam();
try {
param = ZSONObject.stringToClass(dataStr, RequestParam.class);
} catch (RuntimeException e) {
HiLog.error(LABEL, "convert failed.");
}
// 返回结果当前仅支持String,对于复杂结构可以序列化为ZSON字符串上报
Map result = new HashMap();
result.put("code", SUCCESS);
result.put("abilityResult", param.getFirstNum() + param.getSecondNum());
// SYNC
if (option.getFlags() == MessageOption.TF_SYNC) {
reply.writeString(ZSONObject.toZSONString(result));
} else {
// ASYNC
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 result = new HashMap();
result.put("abilityError", ERROR);
reply.writeString(ZSONObject.toZSONString(result));
return false;
}
}
return true;
}
/**
* Internal ability 注册接口。
*/
public static void register(AbilityContext abilityContext) {
instance = new ComputeInternalAbility();
instance.onRegister(abilityContext);
}
private void onRegister(AbilityContext abilityContext) {
this.abilityContext = abilityContext;
this.setInternalAbilityHandler((code, data, reply, option) -> {
return this.onRemoteRequest(code, data, reply, option);
});
}
/**
* Internal ability 注销接口。
*/
public static void unregister() {
instance.onUnregister();
}
private void onUnregister() {
abilityContext = null;
this.setInternalAbilityHandler(null);
}
}
Internal Ability注册:修改继承AceAbility工程中的代码
public class MainAbility extends AceAbility {
@Override
public void onStart(Intent intent) {
// 注册, 如果需要在Page初始化(onInit或之前)时调用AceInternalAbility的能力,注册操作需要在super.onStart之前进行
ComputeInternalAbility.register(this);
...
super.onStart(intent);
}
@Override
public void onStop() {
// 注销
ComputeInternalAbility.unregister();
super.onStop();
}
}