gpt4 book ai didi

flutter - MethodChannel 的后台监听器

转载 作者:行者123 更新时间:2023-12-03 03:22:48 31 4
gpt4 key购买 nike

我的应用程序将文件发送到 ftp 服务器。我通过MethodChannel在android上用java发送文件。

这些部分可以在单线程中完成所有工作。但我想发送带有 AsyncTask (java) 和背景的文件。

此外,我需要使用 MethodChannel 将文件上传结果的结果发送到 Flutter。

我怎样才能做到这一点?
我试图在 AsyncTask 的 doInBackground 上使用 MethodChannel.Result 但它给了我这个错误:

 Caused by: java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: AsyncTask #2

最佳答案

所以为了解决'@UiThread必须在主线程上执行'的错误。

我用过这个

 runOnUiThread(new Runnable() {
@Override
public void run() {
//call the methodChannel.invokeMethod here to avoid @UiThread exception
}
});

我还实现了一个新的 MethodChannel 和一个后台类,用于始终在后台与 native 通信。
class NativeBackground {
final methodChannel = MethodChannel('your.apps.channel/background');

static NativeBackground _instance;
factory NativeBackground() => _instance ? ? = NativeBackground._();
NativeBackground._() {
handlePlatformChannelMethods();
}

Future <void> handlePlatformChannelMethods() async {
methodChannel.setMethodCallHandler((methodCall) {
print('NativeChanell background...');
print(methodCall.method + '(' + methodCall.arguments + ')');
});
}
}

您可以在应用程序的第一个 build() 方法上使用该 NativeBackground 类进行初始化。

关于flutter - MethodChannel 的后台监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61406833/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com