文档管理中心
您当前浏览的HarmonyOS 5.0.1(API 13)文档归档不再维护,推荐您使用最新版本。详细请参考文档维护策略变更
指南应用框架Ability Kit(程序框架服务)Stage模型开发指导应用间跳转拉起指定应用显式Want跳转切换应用链接跳转适配指导

显式Want跳转切换应用链接跳转适配指导

从API 12开始,已不再推荐三方应用使用指定Ability方式(即显式Want)拉起其他应用,推荐通过指定应用链接的方式来实现。

本章节介绍如何从显式Want跳转切换到应用链接跳转。

启动其他应用的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。
    收起
    自动换行
    深色代码主题
    复制
    1. {
    2. "module": {
    3. // ...
    4. "abilities": [
    5. {
    6. // ...
    7. "skills": [
    8. {
    9. "entities": [
    10. "entity.system.browsable"
    11. ],
    12. "actions": [
    13. "ohos.want.action.viewData"
    14. ],
    15. "uris": [
    16. {
    17. "scheme": "https",
    18. "host": "www.example.com",
    19. }
    20. ],
    21. "domainVerify": true
    22. }
    23. ]
    24. }
    25. ]
    26. }
    27. }
  2. 调用方通过openLink接口执行跳转,在接口入参需要传入转换后的link和配置options, 不再传入bundleName、moduleName和abilityName。系统会根据传入的link匹配到符合skill配置的应用。

    • 当options中的appLinkingOnly为true时,匹配到的应用会经过应用市场域名检查(需联网)返回域名校验检查的唯一匹配项或未匹配结果。
    • 当options中的appLinkingOnly为false时,会优先尝试以AppLinking的方式拉起,如果没有匹配的应用则改为使用DeepLinking的方式拉起目标应用。
    收起
    自动换行
    深色代码主题
    复制
    1. import { common } from '@kit.AbilityKit';
    2. import OpenLinkOptions from '@ohos.app.ability.OpenLinkOptions';
    3. import { BusinessError } from '@ohos.base';
    4. import hilog from '@ohos.hilog';
    5. const TAG: string = '[UIAbilityComponentsOpenLink]';
    6. const DOMAIN_NUMBER: number = 0xFF00;
    7. @Entry
    8. @Component
    9. struct Index {
    10. build() {
    11. Button('start link', { type: ButtonType.Capsule, stateEffect: true })
    12. .width('87%')
    13. .height('5%')
    14. .margin({ bottom: '12vp' })
    15. .onClick(() => {
    16. let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
    17. // 通过startAbility接口显式启动其他UIAbility,推荐使用openLink接口。
    18. // let want: Want = {
    19. // bundleName: "com.test.example",
    20. // moduleName: "entry",
    21. // abilityName: "EntryAbility"
    22. // };
    23. // try {
    24. // context.startAbility(want)
    25. // .then(() => {
    26. // hilog.info(DOMAIN_NUMBER, TAG, 'startAbility success.');
    27. // }).catch((err: BusinessError) => {
    28. // hilog.error(DOMAIN_NUMBER, TAG, `startAbility failed. Code is ${err.code}, message is ${err.message}`);
    29. // })
    30. // } catch (paramError) {
    31. // hilog.error(DOMAIN_NUMBER, TAG, `Failed to startAbility. Code is ${paramError.code}, message is ${paramError.message}`);
    32. // }
    33. let link: string = "https://www.example.com";
    34. let openLinkOptions: OpenLinkOptions = {
    35. // 匹配的abilities选项是否需要通过AppLinking域名校验,匹配到唯一配置过的应用ability
    36. appLinkingOnly: true,
    37. // 同want中的parameter,用于传递的参数
    38. parameters: {demo_key: "demo_value"}
    39. };
    40. try {
    41. context.openLink(link, openLinkOptions)
    42. .then(() => {
    43. hilog.info(DOMAIN_NUMBER, TAG, 'open link success.');
    44. }).catch((err: BusinessError) => {
    45. hilog.error(DOMAIN_NUMBER, TAG, `open link failed. Code is ${err.code}, message is ${err.message}`);
    46. })
    47. } catch (paramError) {
    48. hilog.error(DOMAIN_NUMBER, TAG, `Failed to start link. Code is ${paramError.code}, message is ${paramError.message}`);
    49. }
    50. })
    51. }
    52. }

启动其他应用的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。
    收起
    自动换行
    深色代码主题
    复制
    1. {
    2. "module": {
    3. // ...
    4. "abilities": [
    5. {
    6. // ...
    7. "skills": [
    8. {
    9. "entities": [
    10. "entity.system.browsable"
    11. ],
    12. "actions": [
    13. "ohos.want.action.viewData"
    14. ],
    15. "uris": [
    16. {
    17. "scheme": "https",
    18. "host": "www.example.com",
    19. }
    20. ],
    21. "domainVerify": true
    22. }
    23. ]
    24. }
    25. ]
    26. }
    27. }
  2. 调用方通过openLink接口执行跳转,在接口入参需要传入转换后的link和配置options, 不再传入bundleName、moduleName和abilityName。系统会根据传入的link匹配到符合skills配置的应用。AbilityResult回调结果返回通过入参传入回调函数,在启动ability停止自身后返回给调用方的信息。启动成功和失败结果仍通过Promise返回。

    • 当options中的appLinkingOnly为true时,匹配到的应用会经过应用市场域名检查(需联网)返回域名校验检查的唯一匹配项或未匹配结果。
    • 当options中的appLinkingOnly为false时,会优先尝试以AppLinking的方式拉起,如果没有匹配的应用则改为使用DeepLinking的方式拉起目标应用。
    收起
    自动换行
    深色代码主题
    复制
    1. import { common } from '@kit.AbilityKit';
    2. import OpenLinkOptions from '@ohos.app.ability.OpenLinkOptions';
    3. import { BusinessError } from '@ohos.base';
    4. import hilog from '@ohos.hilog';
    5. const TAG: string = '[UIAbilityComponentsOpenLink]';
    6. const DOMAIN_NUMBER: number = 0xFF00;
    7. @Entry
    8. @Component
    9. struct Index {
    10. build() {
    11. Button('start link', { type: ButtonType.Capsule, stateEffect: true })
    12. .width('87%')
    13. .height('5%')
    14. .margin({ bottom: '12vp' })
    15. .onClick(() => {
    16. let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
    17. // 通过startAbility接口显式启动其他UIAbility,推荐使用openLink接口。
    18. // let want: Want = {
    19. // bundleName: "com.test.example",
    20. // moduleName: "entry",
    21. // abilityName: "EntryAbility"
    22. // };
    23. // try {
    24. // context.startAbilityForResult(want)
    25. // .then((data) => {
    26. // hilog.info(DOMAIN_NUMBER, TAG, 'startAbility success. data:' + JSON.stringify(data));
    27. // }).catch((err: BusinessError) => {
    28. // hilog.error(DOMAIN_NUMBER, TAG, `startAbility failed. Code is ${err.code}, message is ${err.message}`);
    29. // })
    30. // } catch (paramError) {
    31. // hilog.error(DOMAIN_NUMBER, TAG, `Failed to startAbility. Code is ${paramError.code}, message is ${paramError.message}`);
    32. // }
    33. let link: string = "https://www.example.com";
    34. let openLinkOptions: OpenLinkOptions = {
    35. // 匹配的abilities选项是否需要通过AppLinking域名校验,匹配到唯一配置过的应用ability
    36. appLinkingOnly: true,
    37. // 同want中的parameter,用于传递的参数
    38. parameters: {demo_key: "demo_value"}
    39. };
    40. try {
    41. context.openLink(link, openLinkOptions, (err, data) => {
    42. // AbilityResult callback回调,仅在被拉起ability死亡时触发
    43. hilog.info(DOMAIN_NUMBER, TAG, 'open link success. Callback result:' + JSON.stringify(data));
    44. }).then(() => {
    45. hilog.info(DOMAIN_NUMBER, TAG, 'open link success.');
    46. }).catch((err: BusinessError) => {
    47. hilog.error(DOMAIN_NUMBER, TAG, `open link failed. Code is ${err.code}, message is ${err.message}`);
    48. })
    49. } catch (paramError) {
    50. hilog.error(DOMAIN_NUMBER, TAG, `Failed to start link. Code is ${paramError.code}, message is ${paramError.message}`);
    51. }
    52. })
    53. }
    54. }
在 指南 中进行搜索
请输入您想要搜索的关键词