最近写Flutter项目的时候用到了url_launcher: ^6.1.10这个版本的插件,电话,短信都没问题,唯独打开外部浏览器一直失败,查阅了相关文档,最终找到了解决方法,直接放结果。
1.第一步,安卓的AndroidManifest.xml 文件需要配置
<queries>
<!-- If your app checks for SMS support -->
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="sms" />
</intent>
<!-- If your app checks for call support -->
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="tel" />
</intent>
<!--#enddocregion android-queries-->
<!-- The "https" scheme is only required for integration tests of this package.
It shouldn't be needed in most actual apps, or show up in the README! -->
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="https" />
</intent>
<!--#docregion android-queries-->
</queries>
第二步,Uri的三个参数都需要填写,直接贴上我封装的结果
```
///通过浏览器加载网页
///host 需要加载的网址(不需要拼接http)
///path 为拼接参数
static toOpenBrowser({required String host,String scheme = "https",String path = ''}) async {
final Uri uri = Uri(scheme: scheme, host: host, path: path);
LogUtils.showObjMsg("要打开的地址:$uri");
if (await canLaunchUrl(uri)) {
await launchUrl(
uri,
mode: LaunchMode.externalApplication,//需要指定打开外部浏览器
);
} else {
ToastUtils.showToast(msg: "打开地址失败,请重试");
}
}
}
```
第三步,调用方式
CommonUtils.toOpenBrowser(host: "www.bilibili.com");