#鸿蒙通关秘籍#如何通过在link字段中传入标准格式的URL,并基于隐式want匹配规则拉起目标UIAbility?

HarmonyOS
2024-12-06 13:51:49
1174浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
银铃笑语SSD
import { common, OpenLinkOptions } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';

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

@Entry
@Component
struct Index {
    build() {
        RelativeContainer() {
            Button("Call StartAbilityForResult")
                .onClick(() => {
                    let `context` = getContext(this) as common.UIAbilityContext;
                    let `link`: string = 'https://www.example.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)}`);
                    }
                })
                .height('100%')
                .width('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.
分享
微博
QQ
微信
回复
2024-12-06 17:13:14
相关问题