Flutter与Native通信(三)BasicMessageChannel

flutter可以native之间可以通过Platform Channels APIs进行通信,API主要有以下三种:

  • MethodChanel:用于传递方法调用(method invocation)
  • EventChannel:用于事件流的发送(event streams)
  • BasicMessageChannel:用于传递字符串和半结构化的消息

BasicMessageChannel用于在flutter和native互相发送消息,一方给另一方发送消息,收到消息之后给出回复。照例我们先看一下API的基本使用流程,然后再看代码实现

1.BasicMessageChannel的基本流程

flutter向native发送消息

  1. [flutter]创建BasicMessageChannel
  2. [native]通过BasicMessageChannel#MessageHandler注册Handler
  3. [flutter]通过BasicMessageChannel#send发送消息
  4. [native]BasicMessageChannel#MessageHandler#onMessage中接收消息,然后reply

native向flutter发送消息

流程也是一样的,只是将[flutter]与[native]反调

2.代码实现

flutter端

flutter需要完成以下工作

  • 创建BasicMessageChannel
  • 通过BasicMessageChannel#send发送消息

相对与其他Channel类型的创建,MessageChannel的创建除了channel名以外,还需要指定编码方式:

1BasicMessageChannel(String name, MessageCodec<T> codec, {BinaryMessenger binaryMessenger}) 2 3

发送的消息会以二进制的形式进行处理,所以要针对不同类型的数进行二进制编码

BinaryCodec 发送二进制消息时 JSONMessageCodec 发送Json格式消息时 StandardMessageCodec 发送基本型数据时 StringCodec 发送String类型消息时

 

 

 

 

 

 

1class _MyHomePageState extends State<MyHomePage> { 2 static const _channel = BasicMessageChannel('com.example.messagechannel/interop', StringCodec()); 3 4 String _platformMessage; 5 6 void _sendMessage() async { 7 final String reply = await _channel.send('Hello World form Dart'); 8 print(reply); 9 } 10 11 @override 12 initState() { 13 super.initState(); 14 15 // Receive messages from platform 16 _channel.setMessageHandler((String message) async { 17 print('Received message = $message'); 18 setState(() => _platformMessage = message); 19 return 'Reply from Dart'; 20 }); 21 22 // Send message to platform 23 _sendMessage(); 24 } 25

native(android)端

  • android端完成以下工作:

  • 创建BasicMessageChannel

  • 通过setHandler注册MessageHandler

  • MessageHandler#onMessage回调中接收到message后,通过reply进行回复

1class MainActivity: FlutterActivity() { 2 override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { 3 GeneratedPluginRegistrant.registerWith(flutterEngine) 4 5 val channel = BasicMessageChannel( 6 flutterEngine.dartExecutor.binaryMessenger, 7 "com.example.messagechannel/interop", 8 StringCodec.INSTANCE) 9 10 // Receive messages from Dart 11 channel.setMessageHandler { message, reply -> 12 Log.d("Android", "Received message = $message") 13 reply.reply("Reply from Android") 14 } 15 16 // Send message to Dart 17 Handler().postDelayed({ 18 channel.send("Hello World from Android") { reply -> 19 Log.d("Android", "$reply") 20 } 21 }, 500) 22 } 23} 24

Andorid端回复的消息会在Flutter端显示

代码交流 2021