#鸿蒙通关秘籍#如何在鸿蒙应用中使用openLink接口进行跳转?

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
PixelPuma

为了在鸿蒙应用中执行应用间跳转,可使用openLink接口。首先,确保目标应用配置了正确的链接跳转规则,其次,使用如下代码:

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

const TAG = '[UIAbilityComponentsOpenLink]'; const DOMAIN_NUMBER = 0xFF00;

@Entry @Component struct Index { build() { Button('start link') .onClick(() => { const context = getContext(this); const link = "https://www.example.com"; const openLinkOptions = { appLinkingOnly: true, parameters: {demo_key: "demo_value"} };

    try {
      context.openLink(link, openLinkOptions)
        .then(() => {
          hilog.info(DOMAIN_NUMBER, TAG, 'open link success.');
        }).catch((err) => {
          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
微信
回复
2天前
相关问题