如何在后台使用Flutter Method Channel(应用最小/关闭) [英] How to use Flutter Method Channel in background (app minimised/closed)
问题描述
我正在使用Flutter应用程序中的本机Android小部件。其中有刷新按钮,单击后必须在Flutter代码中调用一个方法。我正在使用Flutter Method Channel进行通信,当应用程序处于前台时,它工作正常。但是,当应用程序最小化或关闭时,它不起作用。我收到错误 PlatformException(NO_ACTIVITY,null,null)。以下是我的代码。
I am working on a native Android widget in a Flutter App. In which there is refresh button, on click of that I have to call a method in the Flutter code. I am using Flutter Method Channel for the communication and it is working fine when app is in foreground. But it does not work when app is minimised or closed. I get error PlatformException(NO_ACTIVITY, null, null). Below is my code.
Android(AppWidgetProvider)
Android (AppWidgetProvider)
if (methodChannel == null && context != null) {
FlutterMain.startInitialization(context)
FlutterMain.ensureInitializationComplete(context, arrayOf())
// Instantiate a FlutterEngine.
val engine = FlutterEngine(context.applicationContext)
// Define a DartEntrypoint
val entrypoint: DartEntrypoint = DartEntrypoint.createDefault()
// Execute the DartEntrypoint within the FlutterEngine.
engine.dartExecutor.executeDartEntrypoint(entrypoint)
// Register Plugins when in background. When there
// is already an engine running, this will be ignored (although there will be some
// warnings in the log).
//GeneratedPluginRegistrant.registerWith(engine)
methodChannel = MethodChannel(engine.dartExecutor.binaryMessenger, MainActivity.CHANNEL)
}
methodChannel!!.invokeMethod("fetchNewData", "", object : MethodChannel.Result {
override fun notImplemented() {
Toast.makeText(context, "method not implemented", Toast.LENGTH_SHORT).show()
}
override fun error(errorCode: String?, errorMessage: String?, errorDetails: Any?) {
Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show()
}
override fun success(result: Any?) {
Toast.makeText(context, "success", Toast.LENGTH_SHORT).show()
}
})
Flutter
/// calling in main
static Future<void> attachListeners() async {
WidgetsFlutterBinding.ensureInitialized();
var bloc = new AqiCnDashboardBloc();
_channel.setMethodCallHandler((call) {
switch (call.method) {
case 'fetchNewData':
bloc.getAqiCn(false);
return null;
default:
throw MissingPluginException('notImplemented');
}
});
}
推荐答案
我正在收集信息/讨论
void callbackDispatcher() {
WidgetsFlutterBinding.ensureInitialized();
print("Our background job ran!");
}
void main() {
static const MethodChannel _channel = const MethodChannel("channel-name");
Future<void> initialize(final Function callbackDispatcher) async {
final callback = PluginUtilities.getCallbackHandle(callbackDispatcher);
await _channel.invokeMethod('initialize', callback.toRawHandle());
}
}
如此处所述如何在后台运行Flutter?
启动后台作业时本机Flutter引擎未激活。因此,我们无法运行Dart。
Android / iOS可以在后台启动Flutter引擎吗?
是的!我们首先需要注册Dart回调函数,该函数仅在本机代码启动后台作业时才会调用。
此回调函数称为callbackDispatcher。
When a background job is started by native the Flutter engine is not active. So we are unable to run Dart. Can Android/iOS starts the Flutter engine in the background? Yes! We’ll first need to register a Dart callback function which will only be invoked whenever a background job is started by the native code. This callback function is referred to as a callbackDispatcher.
也请查看这些stackoverflow讨论。
Also please check out these stackoverflow discussions.
如何创建Flutter后台服务,该应用程序在应用程序关闭时也可以使用
这篇关于如何在后台使用Flutter Method Channel(应用最小/关闭)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!