HarmonyOS 鸿蒙Next 如何调用系统或其他浏览器来打开链接
HarmonyOS 鸿蒙Next 如何调用系统或其他浏览器来打开链接 请问如何调用系统或其他浏览器来打开链接
3 回复
在HarmonyOS鸿蒙Next中,调用系统或其他浏览器打开链接可以通过Intent
和Ability
来实现。首先,需要创建一个Intent
对象,并设置其Operation
属性为Intent.ACTION_VIEW
,然后通过Uri
对象指定要打开的链接。接着,使用startAbility
方法来启动该Intent
。
示例代码如下:
import featureAbility from '@ohos.ability.featureAbility';
import common from '@ohos.app.ability.common';
let intent = {
action: 'ohos.intent.action.VIEW',
uri: 'https://www.example.com'
};
featureAbility.startAbility(intent).then((data) => {
console.log('Browser opened successfully');
}).catch((error) => {
console.error('Failed to open browser', error);
});
这段代码会调用系统默认的浏览器或其他支持打开链接的应用来访问指定的URL。