鸿蒙开发:通过startAbilityByType拉起垂类应用

通过startAbilityByType拉起垂类应用

使用场景

开发者可通过特定的业务类型如导航、金融等,调用startAbilityByType接口拉起对应的垂域面板,该面板将展示目标方接入的垂域应用,由用户选择打开指定应用以实现相应的垂类意图。垂域面板为调用方提供统一的安全、可信的目标方应用,同时降低调用方的接入成本。

约束限制

设备限制 HarmonyOS NEXT Developer Preview0及以上版本的设备

接口说明

接口startAbilityByType11+  是[UIAbilityContext]和[UIExtensionContentSession]提供的支持基于垂域业务类型拉起垂域面板,调用方通过指定特定的垂域业务类型即可拉起对应的垂域面板,在垂域面板上将展示目标方接入的垂域应用。

type为navigation导航对应的wantParam:

属性名称含义数据类型是否缺省
destinationLatitude终点纬度GCJ-02numbersceneType=1或2时不可缺省
destinationLongitude终点经度GCJ-02numbersceneType=1或2时不可缺省
sceneType意图取值 :1:路线规划 2:导航 3: 地点搜索number可缺省,缺省时默认为1
destinationName终点名称stringsceneType=3时不可缺省
originName起点名称(路线规划场景有效)string可缺省
originLatitude起点纬度GCJ-02(路线规划场景有效)number可缺省
originLongitude起点经度GCJ-02(路线规划场景有效)number可缺省
vehicleType交通出行工具:0:驾车 1:步行 2:骑行 3:公交(路线规划场景有效)number可缺省,缺省时由应用自行处理

接入步骤

调用方接入步骤

  1. 导入ohos.app.ability.common模块。

    import common from '@ohos.app.ability.common';
    
  2. 构造接口参数并调用startAbilityByType接口。

示例

import common from '@ohos.app.ability.common';
let context = getContext(this) as common.UIAbilityContext;
let wantParam: Record<string, Object> = {
    'sceneType':1,
    'destinationLatitude':32.060844,
    'destinationLongitude':118.78315,
    'destinationName':'xx市xx路xx号',
    'originName':'xx市xx公园',
    'originLatitude':31.060844,
    'originLongitude':120.78315,
    'vehicleType':0
};
let abilityStartCallback: common.AbilityStartCallback = {
  onError: (code: number, name: string, message: string) => {
    console.log(`code:` + code + `name:` + name + `message:` + message);
  }
}
context.startAbilityByType("navigation", wantParam, abilityStartCallback, (err) => {
  if (err) {
    console.error(`startAbilityByType fail, err: ${JSON.stringify(err)}`);
  } else {
    console.log(`success`);
  }
});

效果示例图:

效果示例图

目标方接入步骤

  1. 导入ohos.app.ability.UIAbility模块。

    import UIAbility from '@ohos.app.ability.UIAbility';
    
  2. 在module.json5中新增[linkFeature]属性并设置声明当前应用支持的特性功能,从而系统可以从设备已安装应用中找到当前支持该特性的应用。

    配置示例:

    {
    "abilities": [
        {
        "skills": [
            {
            "uris": [
                {
                "scheme": "maps", // 这里仅示意,应用需确保这里声明的的uri能被外部正常拉起
                "host": "navigation",
                "path": "",
                "linkFeature": "navigation" // 声明应用支持导航功能
                },
                {
                "scheme": "maps", // 这里仅示意,应用需确保这里声明的的uri能被外部正常拉起
                "host": "routePlan",
                "path": "",
                "linkFeature": "routePlan" // 声明应用支持路线规划功能
                },
                {
                "scheme": "maps", // 这里仅示意,应用需确保这里声明的的uri能被外部正常拉起
                "host": "search",
                "path": "",
                "linkFeature": "textSearch" // 声明应用支持位置搜索功能
                }
            ]
            }
        ]
        }
    ]
    }
    
  3. 解析参数并做对应处理。

    UIAbility::onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void
    

    在参数want.parameters中会携带Caller方传入的参数(与调用方传入的有些差异),如下表所示:

    属性名称含义数据类型是否缺省
    destinationLatitude终点纬度GCJ-02numbersceneType=1或2时不可缺省
    destinationLongitude终点经度GCJ-02numbersceneType=1或2时不可缺省
    destinationName终点名称stringsceneType=3时不可缺省
    originName起点名称string可缺省,存在时可用于展示路线规划页面
    originLatitude起点纬度GCJ-02number可缺省,存在时可用于展示路线规划页面
    originLongitude起点经度GCJ-02number可缺省,存在时可用于展示路线规划页面
    vehicleType交通出行工具:0:驾车 1:步行 2:骑行 3:公交(路线规划场景有效)number可缺省,缺省时由应用自行处理

    应用可根据[linkFeature]中定义的特性功能,比如路线规划和导航结合接收到的参数开发不同的样式页面。

示例:

import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import hilog from '@ohos.hilog';
import UIAbility from '@ohos.app.ability.UIAbility';
import Want from '@ohos.app.ability.Want';
import window from '@ohos.window';

let destinationLatitude:number;
let destinationLongitude:number;
let originLatitude:number | undefined;
let originLongitude:number | undefined;

export default class EntryAbility extends UIAbility {
    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
        
        destinationLatitude = want.parameters?.destinationLatitude as number;
        destinationLongitude = want.parameters?.destinationLongitude as number;
        originLatitude = want.parameters?.originLatitude as number | undefined;
        originLongitude = want.parameters?.originLongitude as number | undefined;
    }

    onWindowStageCreate(windowStage: window.WindowStage) {
        hilog.info(0x0000, 'testTag', '%{public}s', `Ability onWindowStageCreate: ${JSON.stringify(this.context)}`);

        const storage: LocalStorage = new LocalStorage({
            "destinationLatitude": destinationLatitude,
            "destinationLongitude": destinationLongitude,
            "originLatitude": originLatitude,
            "originLongitude": originLongitude
        } as Record<string, object>);
        
        if(originLatitude !== undefined && originLongitude !== undefined) {
            windowStage.loadContent('pages/IndexForNavigation', storage);
        } else {
            windowStage.loadContent('pages/IndexForRoutePlan', storage);
        }
    }
}
<think>好的,用户问的是关于“startAbilityByType”的介绍。首先,我需要确定这个术语的具体背景,因为它看起来像是某个特定框架或系统中的API方法。根据我的知识库,这可能与华为的HarmonyOS鸿蒙系统)相关,因为HarmonyOS中常用似的方法来启动能力。 接下来,我需要回忆HarmonyOS的文档中关于Ability的概念。在HarmonyOS中,Ability是应用的基本组成单元,分为Page Ability、Service Ability等。启动Ability通常使用startAbility方法,而startAbilityByType可能是其扩展,用于根据型启动能力。 然后,我需要验证这个方法的正确性。查阅相关资料,确认是否存在startAbilityByType方法。如果存在,需要明确它的参数、用法以及适用场景。例如,是否允许开发者通过指定Ability的型(如页面、服务)来启动,而无需明确知道具体的目标Ability。 同时,要考虑用户可能的意图。他们可能是开发者,在开发HarmonyOS应用时遇到问题,需要了解如何使用这个方法。或者他们可能在阅读代码时遇到了这个方法,想了解其功能。需要确保回答结构清晰,步骤详细,便于理解。 还要注意用户提供的系统级指令,比如使用正确的中文和LaTeX格式。虽然这个问题可能不涉及数学公式,但如果有参数或代码示例,需要用正确的格式呈现。 最后,组织回答结构,先介绍方法的作用,再说明使用场景、参数、示例代码以及注意事项,确保用户能够逐步理解并应用该知识。需要避免技术错误,确保信息准确,必要时建议查阅官方文档。</think>在HarmonyOS鸿蒙系统)中,`startAbilityByType` 是一个用于根据预定义型启动Ability的系统级API。以下是关键点解析: 一、基本概念 1. **Ability**:HarmonyOS应用的基础组件,分为: - Page Ability:带UI界面 - Service Ability:后台服务 - Data Ability:数据共享 2. **方法作用**: $$ intent = new Intent() intent.setType("...") startAbilityByType(intent) $$ 通过型匹配而非具体名称启动目标Ability 二、典型使用场景 1. 跨应用协作(如调用系统相机) 2. 模块化解耦(无需硬编码目标Ability名称) 3. 动态服务发现(运行时匹配可用服务) 三、参数配置 ```typescript // 示例代码 let wantInfo = { type: "image/*", // MIME型 action: "ohos.want.action.VIEW", entities: ["entity.system.browsable"] } this.context.startAbilityByType(wantInfo) ``` 四、匹配机制 1. 优先级顺序: ```mermaid graph LR A[精确型匹配] --> B[通配符匹配] --> C[默认兜底] ``` 2. 需在`config.json`声明支持的型: ```json "abilities": [{ "type": "service", "skills": [{ "entities": ["entity.system.home"], "actions": ["action.system.home"] }] }] ``` 五、注意事项 1. 权限控制:需在`config.json`声明所需权限 $$ \text{如:ohos.permission.CAMERA} $$ 2. 生命周期管理:启动Service Ability需注意后台资源占用 3. 错误处理:需捕获`BusinessError`(错误码范围:`20000001-20000005`) 建议开发者结合[官方文档](https://developer.harmonyos.com/cn/docs/documentation/)进行实际调试,可通过DevEco Studio的Hypator工具进行Ability型匹配验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值