如何从Flutter应用程序中打开应用程序? [英] How to open an application from a Flutter app?
问题描述
我正在开发Flutter应用程序,需要在其中向用户显示导航.那么,如何像在Android中使用外部意图那样从Flutter应用程序中打开地图应用程序?
I am developing a Flutter app, in which I need to show navigation to the user for a place. So, how can I open a map application from my Flutter app like we do using external intent in Android?
还是他们有任何Flutter插件?
Or their is any flutter-plugin for it?
推荐答案
我建议您使用 url_launcher 飞镖包.
通过这种方式,您可以使用所有url模式(如您的情况那样打开phone
,sms
甚至maps
).
In this way you can use all url schemas to open (phone
, sms
, and even maps
as in your case).
要在Android和iOS中打开Google Maps,您可以使用常规Android Maps Hemanth Raj所建议的URI模式.
In order to open Google Maps either in Android and iOS you could use the general Android Maps URI schema as suggested by Hemanth Raj.
_openMap() async {
const url = 'https://www.google.com/maps/search/?api=1&query=52.32,4.917';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
如果要在Android上做出选择,则可以使用常规的 geo:
URI模式.
If you want to give a choice on Android you could use the general geo:
URI schema.
If you want specifically open iOS Maps API you can use Cupertino Maps URI schema.
如果您选择区分Android和iOS(不对所有平台使用Google Maps Api架构),则还必须在打开的地图调用中按以下方式进行操作:
If you choose to distinguish between Android and iOS (not using Google Maps Api schema for all platform) you have to do it also in your open map call in a way like this:
_openMap() async {
// Android
const url = 'geo:52.32,4.917';
if (await canLaunch(url)) {
await launch(url);
} else {
// iOS
const url = 'http://maps.apple.com/?ll=52.32,4.917';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
}
或者您可以使用 dart.io
库Platform
类:
Or you can check the OS at runtime with dart.io
library Platform
class:
import 'dart:io';
_openMap() async {
// Android
var url = 'geo:52.32,4.917';
if (Platform.isIOS) {
// iOS
url = 'http://maps.apple.com/?ll=52.32,4.917';
}
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
现在我完成了软管维护(真正的工作……没有一些代码重构……^^'),我可以完成我的回答了.
Now that I finished hosekeeping (the real one... not some code refactoring... ^^') I can finish my answer.
正如我在开始使用url_launcher时告诉您的那样,您可以使用所有URI模式来进行呼叫,发送短信,发送电子邮件等.
As I tell you at the beginning with url_launcher you can use all URI Schemas in order to call, send sms, send e-mail etc.
这里有一些代码可以做到这一点:
Here some code to do that:
_sendMail() async {
// Android and iOS
const uri = 'mailto:test@example.org?subject=Greetings&body=Hello%20World';
if (await canLaunch(uri)) {
await launch(uri);
} else {
throw 'Could not launch $uri';
}
}
_callMe() async {
// Android
const uri = 'tel:+1 222 060 888';
if (await canLaunch(uri)) {
await launch(uri);
} else {
// iOS
const uri = 'tel:001-22-060-888';
if (await canLaunch(uri)) {
await launch(uri);
} else {
throw 'Could not launch $uri';
}
}
}
_textMe() async {
// Android
const uri = 'sms:+39 349 060 888';
if (await canLaunch(uri)) {
await launch(uri);
} else {
// iOS
const uri = 'sms:0039-222-060-888';
if (await canLaunch(uri)) {
await launch(uri);
} else {
throw 'Could not launch $uri';
}
}
}
即使 URI 模式也应该是标准(RFC),有时authority
和
Even if URI schema should be standards (RFC) sometimes the authority
and path
parts of them could differ between frameworks (Android or iOS).
So here I manage the different OSes with exception but, you could do it better with dart.io
library Platform
class:
import 'dart:io'
,然后输入代码:
if (Platform.isAndroid) {
} else if (Platform.isIOS) {
}
我建议您始终在两种环境中对其进行测试.
I suggest you always test them in both environments.
您可以在此处检查Android和iOS架构文档:
You can check the Android and iOS schema documenation here:
- Android
- iOS
如果您想在Android中类似于startActivity(但仅适用于Android平台),则可以使用dart包
If you wanna something similar to startActivity in Android (but that works only for Android platform) you can use the dart package android_intent.
这篇关于如何从Flutter应用程序中打开应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!