简体   繁体   English

如何从 flutter 中的 flutter 代码向 android 本机代码发送数据

[英]How to send data to the android native code from the flutter code in flutter

I'm trying to implement android widgets in android native code of flutter.我正在尝试在 flutter 的 android 本机代码中实现 android 小部件。 I want to send the data from flutter to the android code every 30 mins to update the android widget to show the data.我想每 30 分钟将数据从 flutter 发送到 android 代码,以更新 android 小部件以显示数据。 How do I send data to the android side from flutter side.如何从 flutter 端向 android 端发送数据。

use MethodChannel使用方法通道

static const platform = const MethodChannel('test.flutter.native');

pass to Data(Flutter -> Android)传递给数据(Flutter -> Android)

try {
      final int result = await platform.invokeMethod('nativeCode');
    } catch (e) {
      print("Error : ${e.toString}");
    }

Get Data in Android File获取 Android 文件中的数据

 private static final String CHANNEL = "test.flutter.native";
@Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);

        new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
                new MethodCallHandler() {
                    @Override
                    public void onMethodCall(MethodCall call, Result result) {
                        // Note: this method is invoked on the main thread.
                        // TODO
                    }
                });
    }

See the link for more information有关更多信息,请参阅链接
https://docs.flutter.dev/development/platform-integration/platform-channels?tab=type-mappings-java-tab https://docs.flutter.dev/development/platform-integration/platform-channels?tab=type-mappings-java-tab

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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