在鸿蒙开发中,页面交互时有时候需要调用后台PA的能力,比如数据获取、控制后台等,下面介绍如何做到上述情况。
相关接口:
FA端提供以下三个JS接口:
FeatureAbility.callAbility(OBJECT):调用PA能力。
FeatureAbility.subscribeAbilityEvent(OBJECT, Function):订阅PA能力。
FeatureAbility.unsubscribeAbilityEvent(OBJECT):取消订阅PA能力。
PA端提供以下两类接口:
IRemoteObject.onRemoteRequest(int, MessageParcel, MessageParcel, MessageOption):Ability调用方式,FA使用远端进程通信拉起并请求PA服务。
AceInternalAbility.AceInternalAbilityHandler.onRemoteRequest(int, MessageParcel, MessageParcel, MessageOption):Internal Ability调用方式,采用内部函数调用的方式和FA进行通信。
使用演示:
FA:
- // 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));
- }
- }
- }
复制代码
PA:
- 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<String, Object> result = new HashMap<String, Object>();
- result.put("code", SUCCESS);
- result.put("abilityResult", param.getFirstNum() + param.getSecondNum());
- reply.writeString(ZSONObject.toZSONString(result));
- break;
- }
- default: {
- Map<String, Object> result = new HashMap<String, Object>();
- result.put("abilityError", ERROR);
- reply.writeString(ZSONObject.toZSONString(result));
- return false;
- }
- }
- return true;
- }
-
- @Override
- public IRemoteObject asObject() {
- return this;
- }
- }
- }
复制代码