url_launcher打开外部应用
配置依赖
url_launcher: ^6.0.20
配置权限
/*与application同级*/
<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>
使用
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
class UrlLauncherPage extends StatefulWidget {
UrlLauncherPage({Key? key}) : super(key: key);
@override
State<UrlLauncherPage> createState() => _UrlLauncherPageState();
}
class _UrlLauncherPageState extends State<UrlLauncherPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('打开外部应用'),
),
body: Center(
child: ListView(
children: [
//打开外部浏览器
ElevatedButton(
onPressed: () async {
//设置url
const url = "https://www.tobutomi.top";
if (await canLaunch(url)) {
await launch(url);
} else {
throw "Could not launch $url";
}
},
child: Text('打开外部浏览器')),
//拨打电话
ElevatedButton(
onPressed: () async {
//字符串以tel开头
const tel = "tel:557";
if (await canLaunch(tel)) {
await launch(tel);
} else {
throw "Could not launch $tel";
}
},
child: Text('拨打电话')),
//发送短信
ElevatedButton(
onPressed: () async {
//字符串以sms开头
const sms = "sms:557";
if (await canLaunch(sms)) {
await launch(sms);
} else {
throw "Could not launch $sms";
}
},
child: Text('发送短信')),
//打开外部应用
ElevatedButton(
onPressed: () async {
//应用码
const url = "weixin://";
if (await canLaunch(url)) {
await launch(url);
} else {
throw "Could not launch $url";
}
},
child: Text('打开外部应用')),
],
),
),
);
}
}