flutter 如何从Android服务调用Dart代码?

qkf9rpyu  于 2023-08-07  发布在  Flutter
关注(0)|答案(2)|浏览(152)

我在Android中有一个后台服务,我调用我的Flutter Dart代码使用

var methodChannel = MethodChannel("org.companyname.app.backgroundlocation");
    methodChannel.invokeMethod("startBackgroundService", {
      "encKey" : encryptionKey
    });

字符串
我使用一个名为simple rsa的flutter库来加密位置。然而,Android似乎没有一个等价的。所以,我的问题是:

  • 我可以从我的Android代码调用Dart代码吗?就像我使用Flutter MethodChannel调用Android代码一样?*

我已经检查了this github问题,但没有实际的答案,我可以找到。

ljsrvy3e

ljsrvy3e1#

我不知道它有多有效,但它确实有效:D

private static final String CHANNEL = "widget.filc.hu/timetable";
// You have to run this on the main thread, i think
public static MethodChannel GetMethodChannel(Context context) {
    FlutterMain.startInitialization(context);
    FlutterMain.ensureInitializationComplete(context, new String[0]);

    FlutterEngine engine = new FlutterEngine(context.getApplicationContext());

    DartExecutor.DartEntrypoint entrypoint = new DartExecutor.DartEntrypoint("lib/main.dart", "widget");

    engine.getDartExecutor().executeDartEntrypoint(entrypoint);
    return new MethodChannel(engine.getDartExecutor().getBinaryMessenger(), CHANNEL);
}

字符串
dart端有一个init函数,如下所示:

void widget() async {
    // get preferences everything what you want....
  
    await app.settings.update();
    // Create methodChannel
    const MethodChannel channel = MethodChannel(CHANNEL);
    channel.setMethodCallHandler(
     (call) async {
      final args = call.arguments;

      print('on Dart ${call.method}!');

      switch (call.method) {
      case 'getTimetable':
        return await _getTimetable();
      case 'getRandom':
        return Random().nextInt(100);
      case 'initialize':
        return "HELLO";
      default:
        throw UnimplementedError("Unknow: " + call.method);
    }
  },
);
}


这些链接帮助了我:

6ss1mwsb

6ss1mwsb2#

Follow the steps for enabling a plugin to execute headless Dart code as per https://medium.com/flutter-io/executing-dart-in-the-background-with-flutter-plugins-and-geofencing-2b3e40a1a124. This will involve writing a custom Application class with overrides and a service that have code where a PluginRegistrantCallback will registerWith the plugin registry of a background view (e.g. see here. This typically involves a "foreground" platform channel (for lack of a better description) and a background platform channel being established.
Add code on the Android platform side that will execute Dart code normally (i.e. the "non-headless" way) using the following channel.invokeMethod(...) and handle the call on the Flutter side. In the forked repository linked above, whenever a geofence is registered, the Android side will call channel.invokeMethod("register", null) (see here. This caught in the Flutter to call print the name of the method called, which would be "register" in this case (see here
Run the app and invoke code that execute the logic written in step 2 and notice the Flutter side will catch that a method got called from the platform side. You should method called: register in the debugger output
Now invoke logic that triggers headless execution
Invoke the logic that was done in step 2 again and the Flutter side doesn't get notification a method got called from the platform side i.e. method called: register isn't displayed again

字符串
我注意到这一点,同时将无头执行纳入我自己的插件通知。似乎一旦后台服务调用这个方法,使headless Dart代码能够激发依赖于其他插件的代码,在前台平台通道上调用invokeMethod就不再起作用了。我不知道什么解决方案是在这里,或者可能有一个问题,与引擎本身
[✓] Flutter(Channel beta,v0.9.4,on Mac OS X 10.14 18A391,locale en-AU)· Flutter version 0.9.4 at /Users/michaelbui/flutter · Framework revision f37c235c32(5 weeks ago),2018-09-25 17:45:40 -0400 · Engine revision 74625aed32 · Dart version 2.1.0-dev.5.0.flutter-a2eb050044
[✓] Android工具链-为Android设备开发(Android SDK 27.0.3)· Android SDK位于/Users/michaelbui/Library/Android/sdk · Android NDK位置未配置(可选;对于本机分析支持有用)·平台android-28,build-tools 27.0.3 · ANDROID_HOME = /Users/michaelbui/Library/Android/sdk · Java二进制文件位于:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java· Java版本OpenJDK Runtime Environment(build 1.8.0_152-release-1136-b 06)·接受所有Android许可证。
[✓] iOS toolchain - develop for iOS devices(Xcode 10.1)· Xcode at /Applications/Xcode.app/Contents/Developer· Xcode 10.1,Build version 10B61 · ios-deploy 1.9.2 · CocoaPods version 1.5.0
[✓] Android Studio(版本3.2)· Android Studio at /Applications/Android Studio.app/Contents· Flutter插件版本27.1.1 · Dart插件版本173.4700 · Java版本OpenJDK运行时环境(构建1.8.0_152-release-1136-b 06)
[✓] IntelliJ IDEA社区版(版本2017.3.5)· IntelliJ at /Applications/IntelliJ IDEA CE.app· Flutter插件版本23.0.2 · Dart插件版本173.4700
[✓] VS Code(版本1.28.2)· VS Code at /Applications/Visual Studio Code.app/Contents· Flutter扩展版本2.20.0
[✓]连接的设备(1可用)·像素2 XL ·711 KPXV 0530842·android-arm 64· Android 9(API 28)
·未发现问题!check this out

相关问题