简体   繁体   中英

Send platform message to Android method from background flutter isolate

I want to have a dart background service running forever (isolates) which will communicate with a server through websockets. And I have an API for Android which gather information to send to the server. How can I invoke that Android methods which use callbacks and everything from the background isolate?

EDIT

So far in dart I created an Isolate to periodically invoke the poolSong method in background even if the user is using another app or has the screen turned off.

But that gives me the error below...on the github issues they say that I can't send a platform message from a different Isolate unless it's the main. But if I do it from the main isolate, when the user exit the app that isolate will terminate too.

MainDart

class _MyHomePageState extends State<MyHomePage> {
    final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();
    static const platform = const MethodChannel('mainService');

    static _poolSong(SendPort sendPort) async {
        const oneSec = const Duration(seconds:1);
        new Timer.periodic(oneSec, (Timer t) => platform.invokeMethod('poolSong'));
    }

    @override
    void initState() {
        super.initState();
        ReceivePort receivePort = ReceivePort();
        Isolate.spawn(_poolSong, receivePort.sendPort);
    }
        ·
        ·
        ·

MainActivityJava

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);

    new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
        (call, result) -> {
            if (call.method.equals("startService"))
                startService();
            if (call.method.equals("poolSong"))
                poolSong();
        }
    );
}

ERROR

E/flutter (25412): [ERROR:flutter/runtime/dart_isolate.cc(717)] Isolate (765499726) 'main.dart:_poolSong()' exited with an error
E/flutter (25412): [ERROR:flutter/shell/common/shell.cc(186)] Dart Error: Unhandled exception:
E/flutter (25412): error: native function 'Window_sendPlatformMessage' (4 arguments) cannot be found
E/flutter (25412): #0   Window.sendPlatformMessage (dart:ui/window.dart:811:9)
E/flutter (25412): #1   BinaryMessages._sendPlatformMessage (package:flutter/src/services/platform_messages.dart:39:15)
E/flutter (25412): #2   BinaryMessages.send (package:flutter/src/services/platform_messages.dart:87:12)
E/flutter (25412): #3   MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:286:49)
E/flutter (25412): <asynchronous suspension>
E/flutter (25412): #4   _MyHomePageState._poolSong.<anonymous closure> (package:musictie/main.dart:37:54)
E/flutter (25412): #5   _Timer._runTimers (dart:isolate/runtime/libtimer_impl.dart:382:19)
E/flutter (25412): #6   _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:416:5)
E/flutter (25412): #7   _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)

当应用程序处于垃圾中(应用程序关闭)时尝试运行 dart 代码时会发生此错误,到目前为止,处理后台服务的最佳方法是在本机代码中执行您需要的操作,并且不要尝试从 dart 运行任何内容时应用程序关闭。

Since Flutter 3.7 plugins and platform channels can be used from any isolate, not only from the root one.

References:

After reading the famous article where explain how to use background services in flutter and realize that it is ridiculously complicated I noticed that all my Android code could have been written directly in javascript using the JS version of the API.

So using interactive_webview plugin for flutter I will translate all the android code to js...and that's it. I no longer have to worry about writing specific platform code for iOS and Android. Plus the flutter app runs on background as default (I guess).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM