60Flutter url_launcher

Flutter url_launcher

1.简介

Url_launcher,用于启动Url,打电话,邮件,短信,网页,三方应用

2.使用

dependencies:
  url_launcher: ^6.0.4
import 'package:url_launcher/url_launcher.dart';

案例:

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.red,
      ),
      home: Scaffold(
        appBar: AppBar(title: Text('Url Launcher')),
        body: HomePage(),
      ),
    );
  }
}

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

  @override
  _HomePageState createState() {
    return _HomePageState();
  }
}

class _HomePageState extends State<HomePage> {
  static const url = 'alipays://';//支付宝
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  ///打电话
  _launchTel(String tel) async {
    if (await canLaunch(tel)) {
      await launch(tel);
    } else {
      throw 'Could not launch $tel';
    }
  }

  ///发短信
  _launchSms(String sms) async {
    if (await canLaunch(sms)) {
      await launch(sms);
    } else {
      throw 'Could not launch $sms';
    }
  }

  ///发送邮件
  _launchEmail(String email) async {
    if (await canLaunch(email)) {
      await launch(email);
    } else {
      throw 'Could not launch $email';
    }
  }

  ///打开网址
  _launcherUrl(String url) async {
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  ///进入应用
  _launcherEnterApp(String url) async {
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          OutlinedButton(
              onPressed: () {
                _launchTel('tel:15827452607');
              },
              child: Text('打电话')),
          OutlinedButton(onPressed: () {
            _launcherUrl('https://www.baidu.com/');
          }, child: Text('打开网址')),
          OutlinedButton(
              onPressed: () {
                _launchSms('sms:15827452607');
              },
              child: Text('发短信')),
          OutlinedButton(onPressed: () {
            _launchEmail('mailto:1114625580@qq.com');
          }, child: Text('发邮件')),
          OutlinedButton(onPressed: () {
            _launcherEnterApp(url);
          }, child: Text('进入应用')),
        ],
      ),
    );
  }
}

3.启动第三方应用scheme


<intent-filter>
 <action android:name="android.intent.action.VIEW"/>
 <category android:name="android.intent.category.DEFAULT"/>
 <category android:name="android.intent.category.BROWSABLE"/>
  <data
                    android:scheme="utools"
                    android:host="uih.app"
                    android:path="/params"/>
 </intent-filter>

注意:

  • 建议单独使用,不要和其他filter混用,否则无效。

android中如何获取scheme的信息

 Intent intent = getIntent();
        String scheme = intent.getScheme();
        Uri uri = intent.getData();
        System.out.println("scheme:"+scheme);
        if (uri != null) {
            String host = uri.getHost();
            String dataString = intent.getDataString();
            //获得参数值
            String key1 = uri.getQueryParameter("key1");
           String key2 = uri.getQueryParameter("key2");
     }  

例如:

startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("utools://uih.app/params?key1=0&key2=2")))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值