美文网首页HarmonyOS
从应用内跳转至外部浏览器 - 鸿蒙 HarmonyOS Next

从应用内跳转至外部浏览器 - 鸿蒙 HarmonyOS Next

作者: survivorsfyh | 来源:发表于2024-09-05 20:20 被阅读0次

从应用内跳转至外部浏览器,基于 Want 来实现,同时也可以通过其方式尝试跳转至其它系统模块,具体可参考如下 code :

方法调用

// 调用
pushOutsideWeb(controller, url)

方法实现

import { common, contextConstant, Want } from '@kit.AbilityKit';
import { HintMessage } from '../macros/IMedConfig';
export function pushOutsideWeb(abilityContext: common.UIAbilityContext, url: string) {
  if (url) {
    let context = getContext(abilityContext) as common.UIAbilityContext
    let wantInfo: Want = {
      action: 'ohos.want.action.viewData',
      entities: ['entity.system.browsable'],
      uri: url
    }
    context.startAbility(wantInfo).then(() => {
      console.log('[跳转至外部浏览器] - success')
    }).catch((err: BusinessError) => {
      console.error('[跳转至外部浏览器] - Failed to startAbility. Code: ' + err.code + 'message:' +  err.message);
    })
  } else {
    showToast(HintMessage.HintURLEmpty)
  }
}

以上便是此次分享的全部内容,希望能对大家有所帮助!

相关文章

网友评论

    本文标题:从应用内跳转至外部浏览器 - 鸿蒙 HarmonyOS Next

    本文链接:https://www.haomeiwen.com/subject/fsldljtx.html