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")))