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

Posted 韩曙亮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FlutterFlutter 混合开发 ( Flutter 与 Native 通信 | 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 接收 3 3 3 个参数 :

  • 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.
   */
  @UiThread
  public 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="https://github.com/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.
     */
    @UiThread
    void 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 接口中提供了 3 3 3 个方法 , 根据不同的结果 , 回调不同的接口方法 ;

  • 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="https://github.com/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.
     */
    @UiThread
    void 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.
     */
    @UiThread
    void error(String errorCode, @Nullable String errorMessage, @Nullable Object errorDetails);

    /** Handles a call to an unimplemented method. */
    @UiThread
    void notImplemented();
  }




三、Android 端实现 MethodChannel 通信步骤



Android 端实现 MethodChannel 通信步骤 :

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

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

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

mMethodChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
    @Override
    public 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() {
    @Override
    public void onClick(View v) {
        mMethodChannel.invokeMethod("method", "arguments");
    }
});




四、相关资源



参考资料 :


重要的专题 :


博客源码下载 :

以上是关于FlutterFlutter 混合开发 ( Flutter 与 Native 通信 | Android 端实现 MethodChannel 通信 )的主要内容,如果未能解决你的问题,请参考以下文章

FlutterFlutter 混合开发 ( Flutter 与 Native 通信 | 完整代码示例 )

FlutterFlutter 混合开发 ( Dart 代码调试 | Flutter 单独调试 | 混合模式下 Flutter 调试 )

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

FlutterFlutter 混合开发 ( Flutter 与 Native 通信 | Android 端实现 BasicMessageChannel 通信 )

FlutterFlutter 混合开发 ( Flutter 与 Native 通信 | Android 端实现 EventChannel 通信 )

FlutterFlutter 混合开发 ( 安卓端向 Flutter 传递数据 | FlutterFragment 数据传递 | FlutterActivity 数据传递 )