Flutter:使用url_launcher打开外部浏览器、拨打电话、发送短信、打开第三方app、打开应用商店下载应用

前言

Flutter中的url_launcher是一个用于打开URL的插件。它允许在Flutter应用程序中打开网址、发送电子邮件、拨打电话等操作。使用url_launcher插件,可以轻松地在应用程序中集成各种URL操作。

官方地址
https://pub-web.flutter-io.cn/packages/url_launcher

安装

flutter pub add url_launcher

基本使用

打开网址

Center(
        child: ElevatedButton(
          onPressed: () async{
            final Uri url = Uri.parse('https://www.csdn.net/');
            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.com";
          //邮件主题
          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这就是对应的包名

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无知的小菜鸡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值