本博客与 【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | 在 Flutter 端实现 BasicMessageChannel 通信 ) 博客相对应 , 该博客中开发 Flutter 的 Dart 端 ;
本博客中开发 Android 中的 Java 端 , 最终目标是二者可以进行信息交流 ;
Android 端 Java 中 , BasicMessageChannel 构造函数方法原型如下 :
public final class BasicMessageChannel<T> {
@NonNull private final BinaryMessenger messenger;
@NonNull private final String name;
@NonNull private final MessageCodec<T> 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<T> 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 接收
个参数 :
MessageCodec 消息编解码器的子类实现 : 在 Android Studio 使用 Ctrl + H , 查看 MessageCodec 子类 , 有
个子类 ;
BinaryCodec 实现 :
StringCodec 实现 :
JSONMessageCodec 实现 :
创建了 BasicMessageChannel 实例对象后 , 需要设置信息监听 , 如果要接收 Dart 端发送来的消息 , 需要设置消息处理器 ;
调用 setMessageHandler 方法 , 可以为 BasicMessageChannel 设置一个 消息处理器 ;
BasicMessageChannel.setMessageHandler 函数原型如下 :
/**
* Registers a message handler on this channel for receiving messages sent from the Flutter
* application.
*
* <p>Overrides any existing handler registration for (the name of) this channel.
*
* <p>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<T> handler) {
messenger.setMessageHandler(name, handler == null ? null : new IncomingMessageHandler(handler));
}
设置的 MessageHandler<T> handler 参数 , 就是消息处理器 ;
在 MessageHandler 接口中 , 只有一个 onMessage 方法 , 该方法是用于接收 Dart 传递来的消息的 ;
onMessage 参数简介 :
MessageHandler 接口原型如下 :
/** A handler of incoming messages. */
public interface MessageHandler<T> {
/**
* Handles the specified message received from Flutter.
*
* <p>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.
*
* <p>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.
*
* <p>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<T> reply);
}
BasicMessageChannel 通道向 Dart 发送数据有两个重载的方法 ;
send 方法参数说明 :
send 函数原型 :
public final class BasicMessageChannel<T> {
/**
* 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.
*
* <p>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<T> callback) {
messenger.send(
name,
codec.encodeMessage(message),
callback == null ? null : new IncomingReplyHandler(callback));
}
}
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<String>() {
@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 反馈的信息 ");
}
}
);
}
});
参考资料 :
重要的专题 :
博客源码下载 :
- **Flutter Module 工程 :** [https://github.com/han1202012/flutter\_module](https://github.com/han1202012/flutter_module)
- **Android 应用 :** [https://github.com/han1202012/flutter\_native](https://github.com/han1202012/flutter_native)
- **注意 : 上面两个工程要放在同一个目录中 , 否则编译不通过 ;**