#鸿蒙通关秘籍#使用openLink接口实现应用间跳转的方法

HarmonyOS
2024-12-09 14:37:05
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
网络小魔王

在鸿蒙应用中,使用openLink()接口可以实现应用跳转。首先在openLink接口的link字段中传入目标应用的URL信息,并将options字段中的appLinkingOnly配置为false。代码示例如下:

typescript import { common } from '@kit.AbilityKit'; import OpenLinkOptions from '@ohos.app.ability.OpenLinkOptions'; import { BusinessError } from '@ohos.base'; import hilog from '@ohos.hilog';

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 link: string = "link://www.example.com"; let openLinkOptions: OpenLinkOptions = { appLinkingOnly: false };

    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}`);
    }
  })

} }

分享
微博
QQ
微信
回复
2024-12-09 16:46:55
相关问题
HarmonyOS NAPI接口实现
232浏览 • 1回复 待解决