华为官网上对鸿蒙开发js调用java代码也就是FA调用PA有详细的说明和示例,详见https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ui-js-fa-call-pa-0000001050435961#ZH-CN_TOPIC_0000001063148755__table15473281385

        但文档中对某些说明可能不是很详细,在官方基础之上做一下补充:

        1、对于使用Ability调用方式,继承Ability的类,需要在config.json文件中注册,在abilities中添加如下代码:

"abilities": [
   {
      "icon": "$media:icon",
      //此处填写详细包名+类名
      "name": "com.hongmeng.test.ComputeServiceAbility",
      "description": "ComputeServiceAbility",
      "type": "service"
   }
],

        2、在FA中,在abilityType为0时bundleName和abilityName需要填写Ability对应的模块和类名

        例如类的模块和类名如下所示:

package com.hongmeng.test;
// 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 {
  //省略代码
}

        在FA中就应该是对应的代码:

var actionData = {};
actionData.firstNum = 1024;
actionData.secondNum = 2048;
var action = {};
action.bundleName = 'com.hongmeng.test';
action.abilityName = 'com.hongmeng.test.ComputeServiceAbility';
action.messageCode = 1001;
action.data = actionData;
action.abilityType = 0;
action.syncOption = 0;
var result = await FeatureAbility.callAbility(action);

        3、在FA中,在abilityType为1时bundleName和abilityName需要填写InternalAbility对应注册的InternalAbility实例

        例如在ComputeInternalAbility中注册:

public ComputeInternalAbility() {
    super("com.test","ComputeInternalAbility");
}

        在FA中就应该是对应的代码:

var actionData = {};
actionData.firstNum = 1024;
actionData.secondNum = 2048;
var action = {};
action.bundleName = 'com.test';
action.abilityName = 'ComputeInternalAbility';
action.messageCode = 1001;
action.data = actionData;
action.abilityType = 1;
action.syncOption = 0;
var result = await FeatureAbility.callAbility(action);

        暂时就是这些补充,持续修改中~~

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐