我正在尝试
launch("tel://21213123123")
但是,我收到以下错误!
PlatformException (PlatformException(error, Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?, null))
在这个文件中
message_codecs.dart
这是我的错误日志
E/MethodChannel#plugins.flutter.io/url_launcher(26131): Failed to handle method call
E/MethodChannel#plugins.flutter.io/url_launcher(26131): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
E/MethodChannel#plugins.flutter.io/url_launcher(26131): at android.app.ContextImpl.startActivity(ContextImpl.java:672)
E/MethodChannel#plugins.flutter.io/url_launcher(26131): at android.app.ContextImpl.startActivity(ContextImpl.java:659)
E/MethodChannel#plugins.flutter.io/url_launcher(26131): at android.content.ContextWrapper.startActivity(ContextWrapper.java:331)
E/MethodChannel#plugins.flutter.io/url_launcher(26131): at io.flutter.plugins.urllauncher.UrlLauncherPlugin.onMethodCall(UrlLauncherPlugin.java:61)
E/MethodChannel#plugins.flutter.io/url_launcher(26131): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:200)
E/MethodChannel#plugins.flutter.io/url_launcher(26131): at io.flutter.view.FlutterNativeView.handlePlatformMessage(FlutterNativeView.java:163)
E/MethodChannel#plugins.flutter.io/url_launcher(26131): at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/url_launcher(26131): at android.os.MessageQueue.next(MessageQueue.java:323)
E/MethodChannel#plugins.flutter.io/url_launcher(26131): at android.os.Looper.loop(Looper.java:135)
E/MethodChannel#plugins.flutter.io/url_launcher(26131): at android.app.ActivityThread.main(ActivityThread.java:5468)
E/MethodChannel#plugins.flutter.io/url_launcher(26131): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/url_launcher(26131): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
E/MethodChannel#plugins.flutter.io/url_launcher(26131): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:671)
最佳答案
新版本的 urlLauncher 4.0.2 插件也遇到了同样的问题
我降级到 3.0.3 并且一切正常,因此存储库中可能存在错误。
pubspec.yaml
url_launcher: 3.0.3
示例代码(取自 git repo,但它适用于 3.0.3)
https://github.com/flutter/plugins/tree/master/packages/url_launcher
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
void main() {
runApp(Scaffold(
body: Center(
child: RaisedButton(
onPressed: _launchURL,
child: Text('Show Flutter homepage'),
),
),
));
}
_launchURL() async {
const url = 'https://flutter.io';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
有关插件的所有可用版本,请参见此处。 https://pub.dartlang.org/packages/url_launcher#-changelog-tab-
关于dart - Flutter:从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53497475/