鸿蒙Harmony开发实战(5.0 Beta)startAbilityByType拉起垂类应用实践规范

 鸿蒙开发往期必看:

HarmonyOS NEXT应用开发性能实践总结 

一分钟了解”纯血版!鸿蒙HarmonyOS Next应用开发!

“非常详细的” 鸿蒙HarmonyOS Next应用开发学习路线!(从零基础入门到精通)

 “一杯冰美式的时间” 了解鸿蒙HarmonyOS Next应用开发路径!


使用场景

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

接口说明

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

导航类应用扩展面板参数说明

startAbilityByType接口中type字段为navigation,对应的wantParam参数:

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

说明:

本文中的经纬度均采用GCJ-02坐标系统。

邮件类应用扩展面板参数说明

startAbilityByType接口中type字段为mail,对应的wantParam参数:

属性名称含义数据类型是否必填
email收件人邮箱地址(支持多个且以逗号分隔)string[ ]
cc抄收人邮箱地址(支持多个且以逗号分隔)string[ ]
bcc密送人邮箱地址(支持多个且以逗号分隔)string[ ]
subject邮件主题string
body邮件内容string
ability.params.stream邮件附件(附件的uri地址列表)string[ ]
ability.want.params.uriPermissionFlag给邮件附件赋予至少读权限number否,邮件附件存在情况下必填
sceneType意图取值 :1:发邮件number

说明:

邮件类应用扩展面板中的string或string[ ]类型的内容都需要经过url编码,接收方收到这些参数后需要进行url解码。

拉起导航类应用扩展面板

调用方开发步骤

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

    import { common } from '@kit.AbilityKit';
    
  2. 构造接口参数并调用startAbilityByType接口。

    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. 在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": "PlaceSearch" // 声明应用支持位置搜索功能
                  }
              ]
              }
          ]
          }
      ]
    }
    
  2. 解析参数并做对应处理。

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

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

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

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

完整示例:

import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';

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, number>);

    if (originLatitude !== undefined && originLongitude !== undefined) {
      windowStage.loadContent('pages/IndexForNavigation', storage);
    } else {
      windowStage.loadContent('pages/IndexForRoutePlan', storage);
    }
  }
}

拉起邮件类应用扩展面板

调用方开发步骤

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

    import { common } from '@kit.AbilityKit';
    
  2. 构造接口参数并调用startAbilityByType接口。

    let context = getContext(this) as common.UIAbilityContext;
    let wantParam: Record<string, Object> = {
      'sceneType': 1,
      'email': [encodeURI('xxx@example.com'),encodeURI('xxx@example.com')], // 收件人邮箱地址,多值以逗号分隔,对数组内容使用encodeURI()方法进行url编码
      'cc': [encodeURI('xxx@example.com'),encodeURI('xxx@example.com')], // 抄收人邮箱地址,多值以逗号分隔,对数组内容使用encodeURI()方法进行url编码
      'bcc': [encodeURI('xxx@example.com'),encodeURI('xxx@example.com')], // 密送人邮箱地址,多值以逗号分隔,对数组内容使用encodeURI()方法进行url编码
      'subject': encodeURI('邮件主题'), // 邮件主题,对内容使用encodeURI()方法进行url编码
      'body': encodeURI('邮件正文'), // 邮件正文,对内容使用encodeURI()方法进行url编码
      'ability.params.stream': [encodeURI('附件uri1'),encodeURI('附件uri2')], // 附件uri,多值以逗号分隔,对数组内容使用encodeURI()方法进行url编码
      'ability.want.params.uriPermissionFlag': 1
    };
    let abilityStartCallback: common.AbilityStartCallback = {
      onError: (code: number, name: string, message: string) => {
        console.log(`code:` + code + `name:` + name + `message:` + message);
      }
    }
    
    context.startAbilityByType("mail", wantParam, abilityStartCallback, (err) => {
    if (err) {
        console.error(`startAbilityByType fail, err: ${JSON.stringify(err)}`);
      } else {
        console.log(`success`);
      }
    });
    

    效果示例图:

目标方开发步骤

  1. 在module.json5中新增linkFeature属性并设置声明当前应用支持的特性功能,从而系统可以从设备已安装应用中找到当前支持该特性的应用。

    {
      "abilities": [
          {
          "skills": [
              {
              "uris": [
                  {
                  "scheme": "mail", // 这里仅示意,应用需确保这里声明的的uri能被外部正常拉起
                  "host": "mail",
                  "path": "",
                  "linkFeature": "ComposeMail" // 声明应用支持撰写邮件功能
                  }
              ]
              }
          ]
          }
      ]
    }
    
  2. 解析面板传过来的参数并做对应处理。

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

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

    属性名称含义数据类型是否必填
    email收件人邮箱地址(支持多个且以逗号分隔)string[ ]
    cc抄收人邮箱地址(支持多个且以逗号分隔)string[ ]
    bcc密送人邮箱地址(支持多个且以逗号分隔)string[ ]
    subject邮件主题string
    body邮件内容string
    stream邮件附件列表(附件的uri地址列表)string[ ]

    应用可根据linkFeature中定义的特性功能,比如撰写邮件结合接收到的参数开发不同的样式页面。

完整示例:

import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';

let email: string[] | undefined;
let cc: string[] | undefined;
let bcc: string[] | undefined;
let subject: string | undefined;
let body: string | undefined;
let stream: string[] | undefined;

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
    subject = decodeURI(want.parameters?.subject as string);// 使用decodeURI()方法对邮件主题进行url解码,其他字段处理方法相同
	body = decodeURI(want.parameters?.body as string);// 使用decodeURI()方法对邮件内容进行url解码,其他字段处理方法相同
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    const storage: LocalStorage = new LocalStorage({
      "subject": subject,
      "body": body
    } as Record<string, Object>);

    windowStage.loadContent('pages/IndexForMail', storage);
  }
}

最后

小编在之前的鸿蒙系统扫盲中,我明显感觉到一点,那就是许多人参与鸿蒙开发,但是又不知道从哪里下手,有很多小伙伴不知道学习哪些鸿蒙开发技术?不知道需要重点掌握哪些鸿蒙应用开发知识点?而且学习时频繁踩坑,最终浪费大量时间。所以有一份实用的鸿蒙(HarmonyOS NEXT)路线图、文档、视频、用来跟着学习是非常有必要的。 

如果你是一名有经验的资深Android移动开发、Java开发、前端开发、对鸿蒙感兴趣以及转行人员

鸿蒙全栈开发学习笔记 希望这一份鸿蒙学习文档能够给大家带来帮助~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值