失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | Android 端实现 MethodChannel 通信 )

【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | Android 端实现 MethodChannel 通信 )

时间:2022-10-11 03:11:42

相关推荐

【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | Android 端实现 MethodChannel 通信 )

文章目录

前言一、Android 端 MethodChannel 构造函数二、Android 端 setMethodCallHandler 方法三、Android 端实现 MethodChannel 通信步骤四、相关资源

前言

本博客与 【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | 在 Flutter 端实现 MethodChannel 通信 ) 博客相对应 , 该博客中开发 Flutter 的 Dart 端 ;

本博客中开发 Android 中的 Java 端 , 最终目标是二者可以进行信息交流 ;

一、Android 端 MethodChannel 构造函数

Android 端 Java 中 , MethodChannel 构造函数方法原型如下 :

public class MethodChannel {private static final String TAG = "MethodChannel#";private final BinaryMessenger messenger;private final String name;private final MethodCodec codec;/*** Creates a new channel associated with the specified {@link BinaryMessenger} and with the* specified name and the standard {@link MethodCodec}.** @param messenger a {@link BinaryMessenger}.* @param name a channel name String.*/public MethodChannel(BinaryMessenger messenger, String name) {this(messenger, name, StandardMethodCodec.INSTANCE);}/*** Creates a new channel associated with the specified {@link BinaryMessenger} and with the* specified name and {@link MethodCodec}.** @param messenger a {@link BinaryMessenger}.* @param name a channel name String.* @param codec a {@link MessageCodec}.*/public MethodChannel(BinaryMessenger messenger, String name, MethodCodec codec) {if (BuildConfig.DEBUG) {if (messenger == null) {Log.e(TAG, "Parameter messenger must not be null.");}if (name == null) {Log.e(TAG, "Parameter name must not be null.");}if (codec == null) {Log.e(TAG, "Parameter codec must not be null.");}}this.messenger = messenger;this.name = name;this.codec = codec;}}

BasicMessageChannel 接收 333 个参数 :

BinaryMessenger messenger :用于 发送 / 接收消息 ;String name :Channel 消息通道的名称 , 该名称必须与 Dart 中的消息通道名称相同 ;MethodCodec codec :方法编解码器 ;

二、Android 端 setMethodCallHandler 方法

创建了 MethodChannel 实例对象后 , 如果要接收 Dart 端发送来的消息 , 需要设置 方法回调处理器 ;

调用 setMethodCallHandler 方法 , 可以为 MethodChannel 设置一个 方法回调处理器 ;

MethodChannel.setMethodCallHandler 函数原型如下 :

/*** Registers a method call handler on this channel.** <p>Overrides any existing handler registration for (the name of) this channel.** <p>If no handler has been registered, any incoming method call on this channel will be handled* silently by sending a null reply. This results in a <a* href="https://api.flutter.dev/flutter/services/MissingPluginException-class.html">MissingPluginException</a>* on the Dart side, unless an <a* href="https://api.flutter.dev/flutter/services/OptionalMethodChannel-class.html">OptionalMethodChannel</a>* is used.** @param handler a {@link MethodCallHandler}, or null to deregister.*/@UiThreadpublic void setMethodCallHandler(final @Nullable MethodCallHandler handler) {messenger.setMessageHandler(name, handler == null ? null : new IncomingMethodCallHandler(handler));}

设置的 final @Nullable MethodCallHandler handler 参数 , 就是 方法回调处理器 ;

在 MethodCallHandler 接口中 , 只有一个 onMethodCall 方法 , 该方法是用于接收 Dart 传递来的消息的 ;

void onMethodCall(@NonNull MethodCall call, @NonNull Result result);

onMethodCall 参数简介 :

MethodCall call :Dart 端传递来的消息 ;Result result :向 Dart 端回传的数据 ;

MessageHandler 接口原型如下 :

/** A handler of incoming method calls. */public interface MethodCallHandler {/*** Handles the specified method call received from Flutter.** <p>Handler implementations must submit a result for all incoming calls, by making a single* call on the given {@link Result} callback. Failure to do so will result in lingering Flutter* result handlers. The result may be submitted asynchronously. Calls to unknown or* unimplemented methods should be handled using {@link Result#notImplemented()}.** <p>Any uncaught exception thrown by this method will be caught by the channel implementation* and logged, and an error result will be sent back to Flutter.** <p>The handler is called on the platform thread (Android main thread). For more details see* <a href="/flutter/engine/wiki/Threading-in-the-Flutter-Engine">Threading in* the Flutter Engine</a>.** @param call A {@link MethodCall}.* @param result A {@link Result} used for submitting the result of the call.*/@UiThreadvoid onMethodCall(@NonNull MethodCall call, @NonNull Result result);}

在 MethodCall 中 , 主要有两个成员变量 :

String method :表示调用的方法名 ;Object arguments :表示调用的参数 ;

/** Command object representing a method call on a {@link MethodChannel}. */public final class MethodCall {/** The name of the called method. */public final String method;/*** Arguments for the call.** <p>Consider using {@link #arguments()} for cases where a particular run-time type is expected.* Consider using {@link #argument(String)} when that run-time type is {@link Map} or {@link* JSONObject}.*/public final Object arguments;}

Result 接口中提供了 333 个方法 , 根据不同的结果 , 回调不同的接口方法 ;

void success(@Nullable Object result) :表示调用成功 ;error(String errorCode, @Nullable String errorMessage, @Nullable Object errorDetails) :表示出现错误 ;void notImplemented() :表示要调用的函数在 Dart 端没有实现 ;

/*** Method call result callback. Supports dual use: Implementations of methods to be invoked by* Flutter act as clients of this interface for sending results back to Flutter. Invokers of* Flutter methods provide implementations of this interface for handling results received from* Flutter.** <p>All methods of this class must be called on the platform thread (Android main thread). For* more details see <a* href="/flutter/engine/wiki/Threading-in-the-Flutter-Engine">Threading in the* Flutter Engine</a>.*/public interface Result {/*** Handles a successful result.** @param result The result, possibly null. The result must be an Object type supported by the*codec. For instance, if you are using {@link StandardMessageCodec} (default), please see*its documentation on what types are supported.*/@UiThreadvoid success(@Nullable Object result);/*** Handles an error result.** @param errorCode An error code String.* @param errorMessage A human-readable error message String, possibly null.* @param errorDetails Error details, possibly null. The details must be an Object type*supported by the codec. For instance, if you are using {@link StandardMessageCodec}*(default), please see its documentation on what types are supported.*/@UiThreadvoid error(String errorCode, @Nullable String errorMessage, @Nullable Object errorDetails);/** Handles a call to an unimplemented method. */@UiThreadvoid notImplemented();}

三、Android 端实现 MethodChannel 通信步骤

Android 端实现 MethodChannel 通信步骤 :

首先 , 初始化 MethodChannel 实例对象 ;

MethodChannel mMethodChannel = new MethodChannel(mFlutterFragment.getFlutterEngine().getDartExecutor(), "MethodChannel");

然后 , 为 MethodChannel 实例对象 设置 MethodChannel.MethodCallHandler , 用于接收 Flutter 端调用 Android 端方法 ;

mMethodChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {@Overridepublic void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {show_message.setText("Dart 端通过 MethodChannel 调用 Android 端的 " + call.method + " 方法 , 参数是 " + call.arguments);}});

最后 , 调用 mMethodChannel 的 invokeMethod 方法 , 调用 Flutter 中的方法 ;

findViewById(R.id.channel3).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {mMethodChannel.invokeMethod("method", "arguments");}});

四、相关资源

参考资料 :

Flutter 官网 :https://flutter.dev/Flutter 插件下载地址 :https://pub.dev/packagesFlutter 开发文档 :/docs( 强烈推荐 )官方 GitHub 地址: /flutterFlutter 中文社区 :/Flutter 实用教程 :/docs/cookbookFlutter CodeLab :https://codelabs.flutter-/Dart 中文文档 :/Dart 开发者官网 :https://api.dart.dev/Flutter 中文网 :https://flutterchina.club/ , /docs/Flutter 相关问题 :https://flutterchina.club/faq/ ( 入门阶段推荐看一遍 )GitHub 上的 Flutter 开源示例 :/download/han120/15989510Flutter 实战电子书 :https://book.flutterchina.club/chapter1/Dart 语言练习网站 :/

重要的专题 :

Flutter 动画参考文档 :https://flutterchina.club/animations/

博客源码下载 :

GitHub 地址 :( 随博客进度一直更新 , 有可能没有本博客的源码 )

Flutter Module 工程 :/han120/flutter_moduleAndroid 应用 :/han120/flutter_native注意 : 上面两个工程要放在同一个目录中 , 否则编译不通过 ;

博客源码快照 :/download/han120/21670919 ( 本篇博客的源码快照 , 可以找到本博客的源码 )

如果觉得《【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | Android 端实现 MethodChannel 通信 )》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。