前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Flutter中的打电话、发短信、调起外部浏览器、打开外部APP

Flutter中的打电话、发短信、调起外部浏览器、打开外部APP

作者头像
拉维
发布2019-09-16 16:23:24
5.4K1
发布2019-09-16 16:23:24
举报
文章被收录于专栏:iOS小生活iOS小生活

我们通过url_launcher来实现调起电话、短信、外部浏览器、外部APP的功能。

代码语言:javascript
复制
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class SaveLocalDataPage extends StatefulWidget {
  SaveLocalDataPage({Key key}) : super(key: key);

  _SaveLocalDataPageState createState() => _SaveLocalDataPageState();
}

class _SaveLocalDataPageState extends State<SaveLocalDataPage> {
  //拨打电话
  _call() async {
    const url = 'tel:10000';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  //发送短信
  _message() async {
    const url = 'sms:18868876045';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  //打开外部浏览器
  _openBrower() async {
    const url = 'https://flutter.dev';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  //打开外部应运用
  _openOtherApp() async {
    /**
     * weixin://
     * alipays://
     */
    const url = 'alipays://';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("url_launchDemo")),
      body: Column(
        children: <Widget>[
          RaisedButton(
            onPressed: () {
              _call();
            },
            child: Text("拨打电话"),
          ),
          SizedBox(height: 10),
          RaisedButton(
            onPressed: () {
              _message();
            },
            child: Text("发送短信"),
          ),
          SizedBox(height: 10),
          RaisedButton(
            onPressed: () {
              _openBrower();
            },
            child: Text("打开外部浏览器"),
          ),
          SizedBox(height: 10),
          RaisedButton(
            onPressed: () {
              _openOtherApp();
            },
            child: Text("打开外部应用"),
          ),
        ],
      ),
    );
  }
}

运行效果如下:

需要注意的是,调起外部APP我没有细讲,大家如果有需求可以参考这篇文章:

代码语言:javascript
复制
https://blog.csdn.net/u011272795/article/details/82786027

以上。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-09-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 iOS小生活 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
短信
腾讯云短信(Short Message Service,SMS)可为广大企业级用户提供稳定可靠,安全合规的短信触达服务。用户可快速接入,调用 API / SDK 或者通过控制台即可发送,支持发送验证码、通知类短信和营销短信。国内验证短信秒级触达,99%到达率;国际/港澳台短信覆盖全球200+国家/地区,全球多服务站点,稳定可靠。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档