Flutter打开外部应用可以使用url_launcher这个库
url_launcher官方文档 https://pub.dev/packages/url_launcher
1、dependencies: url_launcher: ^5.4.1
2、引入库
import ‘package:url_launcher/url_launcher.dart’;
3、Flutter中打开外部应用
RaisedButton(
child: Text('打开外部应用'),
onPressed: () async{
// weixin://
const app = 'alipays://';
if (await canLaunch(app)) {
await launch(app);
} else {
throw 'Could not launch $app';
}
},
)
打开其他App
关于打开链接,电话,短信和电子邮件等方式在上面表格中有写,使用方法跟例子一样,不再赘述。
下面我们看一下怎么打开手机中的其他App。
想要打开其他app,需要知道被打开app的scheme, 如果是自己的app,Android可以在Manifest中看到:
<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:host="li.zhuoyuan"
android:scheme="flutter" />
</intent-filter>
上面<data>标签中的就是我们需要知道的url中的东西, 定义了scheme为flutter, host为li.zhuoyuan. 记住这两个字段,在我们想打开这个app的地方需要。可以只定义scheme ,host为可选项。
那么,我们需要的url组成部分就是:scheme://host 如果有host的话。
注意:这里scheme为必填,host、path为选填。选填内容可以不填,但是一旦填上了就必须全部完全匹配才会成功拉起。
我们需要打开的地方执行代码为:
void _openOtherApp() async {
const url = 'flutter://li.zhuoyuan'; //这个url就是由scheme和host组成的 :scheme://host
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}