Flutter opens an external browser, opens an external application, makes a call and sends a text message

Flutter opens an external browser, opens an external application, makes a call and sends a text message

The url_launcher plugin is used.
Plugin address: https://pub.flutter-io.cn/packages/url_launcher

Prepare

insert image description here
The official document gives precautions. From API30 onwards, you need to add a <queries>tag, tag and <application>sibling to AndroidManifest.xml.
The content is as follows:

<queries>
  <!-- If your app opens https URLs -->
  <intent>
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="https" />
  </intent>
  <!-- If your app makes calls -->
  <intent>
    <action android:name="android.intent.action.DIAL" />
    <data android:scheme="tel" />
  </intent>
  <!-- If your sends SMS messages -->
  <intent>
    <action android:name="android.intent.action.SENDTO" />
    <data android:scheme="smsto" />
  </intent>
  <!-- If your app sends emails -->
  <intent>
    <action android:name="android.intent.action.SEND" />
    <data android:mimeType="*/*" />
  </intent>
</queries>

insert image description here

use

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

class UrlLauncherPage extends StatefulWidget {
    
    
  UrlLauncherPage({
    
    Key? key}) : super(key: key);
  _UrlLauncherPageState createState() => _UrlLauncherPageState();
}

class _UrlLauncherPageState extends State<UrlLauncherPage> {
    
    
  @override
  Widget build(BuildContext context) {
    
    
    return Scaffold(
        appBar: AppBar(
          title: Text('UrlLauncher'),
        ),
        body: Center(
            child: Padding(
          padding: EdgeInsets.all(20),
          child: ListView(children: [
            ElevatedButton(
              child: Text('打开外部浏览器'),
              onPressed: () async{
    
                                     
                  const url = 'https://www.itying.com';
                  if (await canLaunch(url)) {
    
    
                    await launch(url);
                  } else {
    
    
                    throw 'Could not launch $url';
                  }
              },
            ),
            SizedBox(height: 10),
            ElevatedButton(
              child: Text('拨打电话'),
              onPressed: () async{
    
    
                  var tel = 'tel:10086';
                  if (await canLaunch(tel)) {
    
    
                    await launch(tel);
                  } else {
    
    
                    throw 'Could not launch $tel';
                  }
                
              },
            ),
            SizedBox(height: 10),
            ElevatedButton(
              child: Text('发送短信'),
              onPressed: () async{
    
    
                 var tel = 'sms:10086';
                  if (await canLaunch(tel)) {
    
    
                    await launch(tel);
                  } else {
    
    
                    throw 'Could not launch $tel';
                  }
              },
            ),
            SizedBox(height: 10),
            ElevatedButton(
              child: Text('打开外部应用'),
              onPressed: () async{
    
    
                  /*
                    weixin://
                    alipays://
                  */
                  var url = 'alipays://';
                  if (await canLaunch(url)){
    
    
                    await launch(url);
                  } else {
    
    
                    throw 'Could not launch $url';
                  }
              },
            )       
          ]),
        )));
  }
}

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

class MyApp extends StatelessWidget {
    
    
  const MyApp({
    
    Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    
    
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: UrlLauncherPage(),
    );
  }
}

Please add image description
Please add image description
Please add image description

Guess you like

Origin blog.csdn.net/m0_46527751/article/details/123237728