HarmonyOS UIAbilityContext.openLink 为 undefined

UIAbilityContext.openLink 不可用,暂时无法使用 openLink 实现应用间跳转

1、新建空项目,在 EntryAbility 的 onCreate 回调中打断点

2、输入 this.context.openLink,结果输出 result=undefined

HarmonyOS
2024-12-23 13:31:40
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

参考以下Demo,使用openLink跳转外部应用

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

const DOMAIN = 0xeeee;
const TAG: string = '[openLinkDemo]';

@Entry
@Component
struct Index {
  @State message: string = 'I am caller';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Button('start browser', { type: ButtonType.Capsule, stateEffect: true })
          .width('87%')
          .height('5%')
          .margin({ bottom: '12vp' })
          .onClick(() => {
            let context = getContext(this) as common.UIAbilityContext;
            let link: string = 'https://www.index.com';
            let openLinkOptions: OpenLinkOptions = {
              appLinkingOnly: true,
              parameters: { demo_key: 'demo_value' }
            };

            try {
              context.openLink(
                link,
                openLinkOptions,
                (err, result) => {
                  hilog.error(DOMAIN, TAG, `openLink callback error.code: ${JSON.stringify(err)}`);
                  hilog.info(DOMAIN, TAG, `openLink callback result: ${JSON.stringify(result.resultCode)}`);
                  hilog.info(DOMAIN, TAG, `openLink callback result data: ${JSON.stringify(result.want)}`);
                }
              ).then(() => {
                hilog.info(DOMAIN, TAG, `open link success.`);
              }).catch((err: BusinessError) => {
                hilog.error(DOMAIN, TAG, `open link failed, errCode ${JSON.stringify(err.code)}`);
              });
            }
            catch (e) {
              hilog.error(DOMAIN, TAG, `exception occured, errCode ${JSON.stringify(e.code)}`);
            }
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
分享
微博
QQ
微信
回复
2024-12-23 14:44:14
相关问题
HarmonyOS SourceTool.xxx值undefined
834浏览 • 1回复 待解决
如何获取单例undefined
1366浏览 • 1回复 待解决
API15中InteractionHand枚举类型undefined
253浏览 • 0回复 待解决
如何获取UIAbilityContext
2614浏览 • 1回复 待解决
如何在UI中获取UIAbilityContext
3619浏览 • 1回复 待解决
HarmonyOS List提示undefined
458浏览 • 1回复 待解决