admin管理员组文章数量:1624346
前言
Flutter中的url_launcher
是一个用于打开URL的插件。它允许在Flutter应用程序中打开网址、发送电子邮件、拨打电话等操作。使用url_launcher
插件,可以轻松地在应用程序中集成各种URL操作。
官方地址
https://pub-web.flutter-io/packages/url_launcher
安装
flutter pub add url_launcher
基本使用
打开网址
Center(
child: ElevatedButton(
onPressed: () async{
final Uri url = Uri.parse('https://www.csdn/');
if (!await launchUrl(url,mode:LaunchMode.externalApplication)) {
throw Exception('Could not launch $url');
}
},
child: const Text("打开浏览器"),
)
)
这里有两个注意点:
- 模拟器里无法正常运行,需要使用真机
- 必须设置为
mode:LaunchMode.externalApplication
,否则会在应用内单独打开一个界面进行显示
电子邮件
Center(
child: ElevatedButton(
onPressed: () async {
// 收件人邮箱
String recipient = "15065845632@163";
//邮件主题
String subject = "邮件主题";
// 邮件内容
String body = "邮件内容";
String mailtoUri =
"mailto:$recipient?subject=$subject&body=$body";
final Uri url = Uri.parse(mailtoUri);
if (!await launchUrl(url, mode: LaunchMode.externalApplication)) {
throw Exception('Could not launch $mailtoUri');
}
},
child: const Text("发邮件"),
))
发短信
Center(
child: ElevatedButton(
onPressed: () async {
// 收件人电话
String recipient = "10086";
// 短信内容
String body = "1";
String smsUrl = 'sms:$recipient?body=${Uri.encodeQueryComponent(body)}';
final Uri url = Uri.parse(smsUrl);
if (!await launchUrl(url, mode: LaunchMode.externalApplication)) {
throw Exception('Could not launch $smsUrl');
}
},
child: const Text("发短信"),
))
打开第三方app
Center(
child: ElevatedButton(
onPressed: () async {
final Uri url = Uri.parse('weixin://');
if (!await launchUrl(url, mode: LaunchMode.externalApplication)) {
throw Exception('Could not launch $url');
}
},
child: const Text("打开微信"),
))
微信可以正常打开,下面这些是从网上搜集的,不清楚是否好用
QQ: mqq://
微信: weixin://
京东: openapp.jdmoble://
淘宝: taobao://
美团: imeituan://
支付宝: alipay://
微博: sinaweibo://
知乎: zhihu://
豆瓣fm: doubanradio://
网易公开课: ntesopen://
Chrome: googlechrome://
QQ浏览器: mqqbrowser://
uc浏览器: ucbrowser://
搜狗浏览器: SogouMSE://
百度地图: baidumap:// bdmap://
优酷: youku://
有道词典: yddictproapp://
QQ音乐:qqmusic://
腾讯视频:tenvideo://
网易云音乐:orpheus://
应用商店
小米商店:"mimarket://details?id=com.xX.XX"
华为商店:"appmarket://details?id=com.xx.xx"
oppo商店:"oppomarket://details?packagename=com.xx.XX"
vivo商店:""vivomarket://details?id=com.xx.Xx"
以使用oppo商店打开美团为例
ElevatedButton(
onPressed: () async {
final Uri url = Uri.parse('oppomarket://details?packagename=com.sankuai.meituan');
if (!await launchUrl(url, mode: LaunchMode.externalApplication)) {
throw Exception('Could not launch $url');
}
},
child: const Text("下载美团"),
))
要想在应用商店打开,需要获取到包名,需要先下载一个应用宝(只要你的应用市场支持分享就行)
我这里分享到了微信里,如下图
打开
复制链接,你会得到这样的链接:
https://a.app.qq.com/o/simple.jsp?pkgname=com.sankuai.meituan&fromcase=70051&g_f=1182517&scenevia=XQYFX
com.sankuai.meituan
这就是对应的包名
版权声明:本文标题:Flutter:使用url_launcher打开外部浏览器、拨打电话、发送短信、打开第三方app、打开应用商店下载应用 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1728908215a1179012.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论