#鸿蒙通关秘籍#如何获取通过链接跳转的结果?

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
紫气东来MOQ

要在鸿蒙中启动外部应用并获取返回结果,使用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, (err, data) => {
        hilog.info(DOMAIN_NUMBER, TAG, 'open link success. Callback result:' + JSON.stringify(data));
      }).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
微信
回复
3天前
相关问题