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

HarmonyOS
6天前
浏览
收藏 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%')
        }
    }
}
分享
微博
QQ
微信
回复
6天前
相关问题
#鸿蒙学习大百科#什么是Want
321浏览 • 1回复 待解决