鸿蒙Harmony开发实战(5.0 Beta)显式Want跳转切换应用链接跳转规范详解

 鸿蒙开发往期必看:

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

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

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


启动其他应用的UIAbility

  1. 将待跳转的应用安装到设备,在其对应UIAbility的module.json5配置文件中配置skills标签的entities字段、actions字段和uri字段:

    • “actions"列表中包含"ohos.want.action.viewData”。
    • “entities"列表中包含"entity.system.browsable”。
    • "uris"列表中包含"scheme"为"https"且"domainVerify"为true的元素。uri的匹配规则参考uri匹配, domainVerify为true代表开启域名检查,通过applinking匹配该应用时需经过配置的域名校验后才能匹配到。applinking域名配置具体可参考AppLinking。
    {
      "module": {
        "abilities": [
          {
            "name": "EntryAbility",
            "srcEntry": "./ets/entryability/EntryAbility.ets",
            "icon": "$media:icon",
            "label": "$string:EntryAbility_label",
            "startWindowIcon": "$media:icon",
            "startWindowBackground": "$color:start_window_background",
            "skills": [
              {
                "entities": [
                  "entity.system.browsable"
                ],
                "actions": [
                  "ohos.want.action.viewData"
                ],
                "uris": [
                  {
                    "scheme": "https",
                    "host": "www.example.com",
                  }
                ],
              "domainVerify": true
              }
            ]
          }
        ]
      }
    }
    
  2. 调用方通过openLink接口执行跳转,在接口入参需要传入转换后的link和配置options, 不再传入bundleName、moduleName和abilityName。系统会根据传入的link匹配到符合skill配置的应用。

    • 当options中的appLinkingOnly为true时,匹配到的应用会经过应用市场域名检查(需联网)返回域名校验检查的唯一匹配项或未匹配结果。
    • 当options中的appLinkingOnly为false时,会优先尝试以AppLinking的方式拉起,如果没有匹配的应用则改为使用DeepLinking的方式拉起目标应用。

    具体请参考AppLinking开发指南。

    import { common, OpenLinkOptions } from '@kit.AbilityKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    
    const TAG: string = '[UIAbilityComponentsOpenLink]';
    const DOMAIN_NUMBER: number = 0xFF00;
    
    @Entry
    @Component
    struct Index {
      build() {
        Button('start link', { type: ButtonType.Capsule, stateEffect: true })
          .width('87%')
          .height('5%')
          .margin({ bottom: '12vp' })
          .onClick(() => {
            let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
            // let want: Want = {
            //   bundleName: "com.test.example",
            //   moduleName: "entry",
            //   abilityName: "EntryAbility"
            // };
            // try {
            //   context.startAbility(want)
            //     .then(() => {
            //       hilog.info(DOMAIN_NUMBER, TAG, 'startAbility success.');
            //     }).catch((err: BusinessError) => {
            //       hilog.error(DOMAIN_NUMBER, TAG, `startAbility failed. Code is ${err.code}, message is ${err.message}`);
            //     })
            // } catch (paramError) {
            //   hilog.error(DOMAIN_NUMBER, TAG, `Failed to startAbility. Code is ${paramError.code}, message is ${paramError.message}`);
            // }
            let link: string = "https://www.example.com";
            let openLinkOptions: OpenLinkOptions = {
              // 匹配的abilities选项是否需要通过AppLinking域名校验,匹配到唯一配置过的应用ability
              appLinkingOnly: true,
              // 同want中的parameter,用于传递的参数
              parameters: {demo_key: "demo_value"}
            };
    
            try {
              context.openLink(link, openLinkOptions)
                .then(() => {
                  hilog.info(DOMAIN_NUMBER, TAG, 'open link success.');
                }).catch((err: BusinessError) => {
                  hilog.error(DOMAIN_NUMBER, TAG, `open link failed. Code is ${err.code}, message is ${err.message}`);
                })
            } catch (paramError) {
              hilog.error(DOMAIN_NUMBER, TAG, `Failed to start link. Code is ${paramError.code}, message is ${paramError.message}`);
            }
          })
      }
    }
    

启动其他应用的UIAbility并获取返回结果

  1. 将待跳转的应用安装到设备,在其对应UIAbility的module.json5配置文件中配置skills标签的entities字段、actions字段和uri字段:

    • “actions"列表中包含"ohos.want.action.viewData”。
    • “entities"列表中包含"entity.system.browsable”。
    • "uris"列表中包含"scheme"为"https"且"domainVerify"为true的元素。uri的匹配规则参考uri匹配, domainVerify为true代表开启域名检查,通过applinking匹配该应用时需经过配置的域名校验后才能匹配到。applinking域名配置具体可参考App Linking。
    {
      "module": {
        "abilities": [
          {
            "name": "EntryAbility",
            "srcEntry": "./ets/entryability/EntryAbility.ets",
            "icon": "$media:icon",
            "label": "$string:EntryAbility_label",
            "startWindowIcon": "$media:icon",
            "startWindowBackground": "$color:start_window_background",
            "skills": [
              {
                "entities": [
                  "entity.system.browsable"
                ],
                "actions": [
                  "ohos.want.action.viewData"
                ],
                "uris": [
                  {
                    "scheme": "https",
                    "host": "www.example.com",
                  }
                ],
              "domainVerify": true
              }
            ]
          }
        ]
      }
    }
    
  2. 调用方通过openLink接口执行跳转,在接口入参需要传入转换后的link和配置options, 不再传入bundleName、moduleName和abilityName。系统会根据传入的link匹配到符合skills配置的应用。AbilityResult回调结果返回通过入参传入回调函数,在启动ability停止自身后返回给调用方的信息。启动成功和失败结果仍通过Promise返回。

    • 当options中的appLinkingOnly为true时,匹配到的应用会经过应用市场域名检查(需联网)返回域名校验检查的唯一匹配项或未匹配结果。
    • 当options中的appLinkingOnly为false时,会优先尝试以AppLinking的方式拉起,如果没有匹配的应用则改为使用DeepLinking的方式拉起目标应用。
      具体请参考AppLinking开发指南。
    import { common, OpenLinkOptions } from '@kit.AbilityKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    
    const TAG: string = '[UIAbilityComponentsOpenLink]';
    const DOMAIN_NUMBER: number = 0xFF00;
    
    @Entry
    @Component
    struct Index {
      build() {
        Button('start link', { type: ButtonType.Capsule, stateEffect: true })
          .width('87%')
          .height('5%')
          .margin({ bottom: '12vp' })
          .onClick(() => {
            let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
            // let want: Want = {
            //   bundleName: "com.test.example",
            //   moduleName: "entry",
            //   abilityName: "EntryAbility"
            // };
            // try {
            //   context.startAbilityForResult(want)
            //     .then((data) => {
            //       hilog.info(DOMAIN_NUMBER, TAG, 'startAbility success. data:' + JSON.stringify(data));
            //     }).catch((err: BusinessError) => {
            //       hilog.error(DOMAIN_NUMBER, TAG, `startAbility failed. Code is ${err.code}, message is ${err.message}`);
            //     })
            // } catch (paramError) {
            //   hilog.error(DOMAIN_NUMBER, TAG, `Failed to startAbility. Code is ${paramError.code}, message is ${paramError.message}`);
            // }
            let link: string = "https://www.example.com";
            let openLinkOptions: OpenLinkOptions = {
              // 匹配的abilities选项是否需要通过AppLinking域名校验,匹配到唯一配置过的应用ability
              appLinkingOnly: true,
              // 同want中的parameter,用于传递的参数
              parameters: {demo_key: "demo_value"}
            };
    
            try {
              context.openLink(link, openLinkOptions, (err, data) => {
                // AbilityResult callback回调,仅在被拉起ability死亡时触发
                hilog.info(DOMAIN_NUMBER, TAG, 'open link success. Callback result:' + JSON.stringify(data));
              }).then(() => {
                hilog.info(DOMAIN_NUMBER, TAG, 'open link success.');
              }).catch((err: BusinessError) => {
                hilog.error(DOMAIN_NUMBER, TAG, `open link failed. Code is ${err.code}, message is ${err.message}`);
              })
            } catch (paramError) {
              hilog.error(DOMAIN_NUMBER, TAG, `Failed to start link. Code is ${paramError.code}, message is ${paramError.message}`);
            }
          })
      }
    }

最后

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值