一、Android 端 BasicMessageChannel 构造函数
Android 端 Java 中 , BasicMessageChannel 构造函数方法原型如下 :
public final class BasicMessageChannel {
@NonNull private final BinaryMessenger messenger;
@NonNull private final String name;
@NonNull private final MessageCodec codec;
/**
* Creates a new channel associated with the specified {@link BinaryMessenger} and with the
* specified name and {@link MessageCodec}.
*
* @param messenger a {@link BinaryMessenger}.
* @param name a channel name String.
* @param codec a {@link MessageCodec}.
*/
public BasicMessageChannel(
@NonNull BinaryMessenger messenger, @NonNull String name, @NonNull MessageCodec 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 33 个参数 :
BinaryMessenger messenger : 用于 发送 / 接收消息 ;
String name : Channel 消息通道的名称 , 该名称必须与 Dart 中的消息通道名称相同 ;
MessageCodec
二、Android 端 MessageCodec 子类实现
MessageCodec 消息编解码器的子类实现 : 在 Android Studio 使用 Ctrl + H , 查看 MessageCodec 子类 , 有 4 44 个子类 ; BinaryCodec : 二进制编解码器 , 返回值类型 和 入参类型 都是二进制格式 , 即 Byte 数组 ; 编解码器没有做任何操作 , 原封不动的传递二进制数据 ; 支持 二进制数据 ; 适用场景 : 传递大量的二进制数据 , 如图片 , 音视频等 , 可以直接传递内存块 , 不用再进行编解码 , 导致消耗不必要的性能 ; StringCodec : 二进制 Byte 数组与字符串之间进行编解码 , 字符串编码格式 UTF-8 ; 发送的时候是 String 类型 , 经过 Channel 通道时编码成二进制类型 , 接收时在解码成 String 类型 ; 支持 字符串 数据 ; JSONMessageCodec : 二进制数据 与 基础数据 之间进行编解码 , 支持 基础数据类型 / 列表 / 字典 ; StandardMessageCodec : BasicMessageChannel 消息通道的 默认编码器 ; 支持 基础数据类型 / 二进制数据 / 列表 / 字典 BinaryCodec 实现 : Android : ByteBuffer ; iOS : NSData ; Flutter : Uint8List ; StringCodec 实现 : Android : java.lang.String ; iOS : NSString ; Flutter : String ; JSONMessageCodec 实现 : Android : SONUtil , StringCodec ; iOS : NSJSONSerialization ;
三、Android 端 setMessageHandler 方法
创建了 BasicMessageChannel 实例对象后 , 需要设置信息监听 , 如果要接收 Dart 端发送来的消息 , 需要设置消息处理器 ;
调用 setMessageHandler 方法 , 可以为 BasicMessageChannel 设置一个 消息处理器 ;
BasicMessageChannel.setMessageHandler 函数原型如下 :
/**
* Registers a message handler on this channel for receiving messages sent from the Flutter
* application.
*
* Overrides any existing handler registration for (the name of) this channel.
*
*
If no handler has been registered, any incoming message on this channel will be handled
* silently by sending a null reply.
*
* @param handler a {@link MessageHandler}, or null to deregister.
*/
@UiThread
public void setMessageHandler(@Nullable final MessageHandler handler) {
messenger.setMessageHandler(name, handler == null ? null : new IncomingMessageHandler(handler));
}
设置的 MessageHandler
在 MessageHandler 接口中 , 只有一个 onMessage 方法 , 该方法是用于接收 Dart 传递来的消息的 ;
onMessage 参数简介 :
T message : Dart 端传递来的消息 ;
Reply
MessageHandler 接口原型如下 :
/** A handler of incoming messages. */
public interface MessageHandler {
/**
* Handles the specified message received from Flutter.
*
* Handler implementations must reply to all incoming messages, by submitting a single reply
* message to the given {@link Reply}. Failure to do so will result in lingering Flutter reply
* handlers. The reply may be submitted asynchronously.
*
*
Any uncaught exception thrown by this method, or the preceding message decoding, will be
* caught by the channel implementation and logged, and a null reply message will be sent back
* to Flutter.
*
*
Any uncaught exception thrown during encoding a reply message submitted to the {@link
* Reply} is treated similarly: the exception is logged, and a null reply is sent to Flutter.
*
* @param message the message, possibly null.
* @param reply a {@link Reply} for sending a single message reply back to Flutter.
*/
void onMessage(@Nullable T message, @NonNull Reply reply);
}
四、Android 端 send 方法
BasicMessageChannel 通道向 Dart 发送数据有两个重载的方法 ;
void send(@Nullable T message) 方法 : 单纯的向 Dart 端发送数据 , 不接受返回的数据 ;
void send(@Nullable T message, @Nullable final Reply
send 方法参数说明 :
T message 参数 : 要发送给 Dart 端的数据 ;
final Reply
send 函数原型 :
public final class BasicMessageChannel {
/**
* Sends the specified message to the Flutter application on this channel.
*
* @param message the message, possibly null.
*/
public void send(@Nullable T message) {
send(message, null);
}
/**
* Sends the specified message to the Flutter application, optionally expecting a reply.
*
* Any uncaught exception thrown by the reply callback will be caught and logged.
*
* @param message the message, possibly null.
* @param callback a {@link Reply} callback, possibly null.
*/
@UiThread
public void send(@Nullable T message, @Nullable final Reply callback) {
messenger.send(
name,
codec.encodeMessage(message),
callback == null ? null : new IncomingReplyHandler(callback));
}
}
五、Android 端实现 BasicMessageChannel 通信步骤
Android 端实现 BasicMessageChannel 通信步骤 :
首先 , 获取 FlutterEngine 实例对象 , 需要从该实例对象中获取 BinaryMessenger ; 这里从 FlutterFragment 中获取 , 从 FlutterActivity 中也可以获取 ;
FlutterFragment mFlutterFragment = FlutterFragment.withNewEngine().
initialRoute("嵌入 FlutterFragment").build();
mFlutterFragment.getFlutterEngine();
然后 , 构建 BasicMessageChannel 对象 , 传入如下参数 :
// 初始化
BasicMessageChannel mBasicMessageChannel = new BasicMessageChannel(
mFlutterFragment.getFlutterEngine().getDartExecutor(),
"BasicMessageChannel",
StringCodec.INSTANCE);
在后 , 设置消息接收监听 , 监听从 Dart 端传递来的消息 , 如果有消息传来 , 会自动回调 MessageHandler 中的 onMessage 方法 ;
// 设置消息接收监听
mBasicMessageChannel.setMessageHandler(new BasicMessageChannel.MessageHandler() {
@Override
public void onMessage(@Nullable String message, @NonNull BasicMessageChannel.Reply reply) {
show_message.setText("Dart 通过 BasicMessageChannel 通道向 Native 发送 " + message + " 信息");
}
});
最后 , 设置发送消息 , 点击按钮后 , 即可向 Dart 端发送消息 , 同时设置 Reply 参数 , 如果 Dart 端有回送反馈 , 则自动回调 BasicMessageChannel.Reply 接口中的 void reply(@Nullable Object reply) 方法 ;
// 点击按钮发送消息 , 并设置 Reply 接收 Dart 返回的消息
findViewById(R.id.channel1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBasicMessageChannel.send(
"Native 通过 BasicMessageChannel 通道发送消息 Hello !",
new BasicMessageChannel.Reply() {
@Override
public void reply(@Nullable Object reply) {
show_message.setText("Native 通过 BasicMessageChannel 通道发送消息 Hello 后 , Dart 反馈的信息 ");
}
}
);
}
});