Flutter 与 Android原生 相互通信:BasicMessageChannel、MethodChannel、EventChannel

前言

本文主要讲解,使用不同的 Channel 让 Flutter 和 Android原生 进行通信,由于只是讲解两端通信,所以可视化效果不好

不过我写了一篇专门讲解 Flutter 嵌入 Android原生View的文章

Flutter 页面嵌入 Android原生 View-CSDN博客

可以先看完,再结合案例自己改动一下,做到可视化,不过我还是建议优先看当前文章

我采用在Android原生延时发送方式,来模拟Android原生向Flutter 发送消息。

1、BasicMessageChannel

主要应用于传输数据的通道,目前提供了以下几种消息类型:

JSONMessageCodecStandardMessageCodecStringCodecBinaryCodec

默认使用的消息类型是 StandardMessageCodec

下面图片是 Flutter和Android原生,分别支持的数据类型 和 对应关系;

1.1 JSONMessageCodec

Json字符串。

1.2 StandardMessageCodec

标准类型,可以看作默认类型,支持的类型也是最多的。

1.3 StringCodec

字符串。

1.4 BinaryCodec

二进制,比如传输文件。

我挑了两个最常用的类型进行演示,比如Flutter向Android原生发送请求,Android原生收到请求后将数据响应给Flutter;

BasicMessageChannel的监听回调,没有提供具体调用了哪个函数的标识,所以在演示PUT时,我会先将GET相关代码注释,反之也一样;

如果和其他Channel配合使用可以解决这个注释问题, 比如BasicMessageChannel 和 MethodChannel 一起使用,这个案例我放在最后讲解,这一阶段先麻烦一下;

传输 BinaryCodec,我使用的是音频文件,传输完成默认开始播放,友情提示,戴耳机或者声音小点,避免社S

在Flutter添加音频播放库,库的具体地址

just_audio | Flutter Package

just_audio: ^0.9.36

Android原生 需要的配置,app/build.gradle

defaultConfig {... ...multiDexEnabled true
}dependencies {def exoplayer_version = "2.18.5"implementation "com.google.android.exoplayer:exoplayer-core:$exoplayer_version"implementation "com.google.android.exoplayer:exoplayer-dash:$exoplayer_version"implementation "com.google.android.exoplayer:exoplayer-hls:$exoplayer_version"implementation "com.google.android.exoplayer:exoplayer-smoothstreaming:$exoplayer_version"implementation 'androidx.multidex:multidex:2.0.1'
}

传输JSONMessageCodec

传输BinaryCodec

1.5 传输JSONMessageCodec

Flutter:main_json_basic_message_channel.dart

import 'dart:math';import 'package:flutter/material.dart';
import 'package:flutter/services.dart';void main() {runApp(const MyApp());
}class MyApp extends StatelessWidget {const MyApp({super.key});@overrideWidget build(BuildContext context) {return MaterialApp(debugShowCheckedModeBanner: false,title: 'Flutter Demo',theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),useMaterial3: true,),home: const MyHomePage(title: 'Flutter Demo Home Page'),);}
}/// BasicMessageChannel
/// 使用Map类型,对应 Android端的 JSONObject类型
class MyHomePage extends StatefulWidget {const MyHomePage({super.key, required this.title});final String title;@overrideState<MyHomePage> createState() => _MyHomePageState();
}class _MyHomePageState extends State<MyHomePage> {late BasicMessageChannel channel;// Android原生View 在Flutter引擎上注册的唯一标识,在Flutter端使用时必须一样static const String CHANNEL_NAME = 'flutter.mix.android/json_basic_message_channel';String msgState = "默认"; // 消息传递状态@overrideinitState() {super.initState();initChannel();}/// 初始化消息通道initChannel() {channel = const BasicMessageChannel(CHANNEL_NAME,JSONMessageCodec()); // 创建 Flutter端和Android端的,相互通信的通道// 监听来自 Android端 的消息通道// Android端调用了函数,这个handler函数就会被触发channel.setMessageHandler(handler);}/// 监听来自 Android端 的消息通道/// Android端调用了函数,这个handler函数就会被触发Future<dynamic> handler(dynamic message) async {// PUTvar androidCount = message['androidNum'];msgState = 'Flutter端接收Android端PUT请求成功,数据:$androidCount';setState(() {});return 0; // 返回给Android端// GET,这里模拟在Android端显示// var randomV = getRandomV();// Map<String, int> map = {'flutterNum': randomV};// msgState = 'Flutter端接收Android端GET请求成功:$randomV';// setState(() {});// return map; // 返回给Android端}/// Flutter端 向 Android端 发送数据,PUT 操作flutterSendAndroidData() {var randomV = getRandomV();Map<String, int> map = {'flutterNum': randomV};// Android端调用Reply相关回调函数后,then、catchError 会接收到channel.send(map).then((value) {var flutterNum = value['flutterNum'];msgState = 'Android端接收Flutter端PUT请求成功,数据:$flutterNum ----> 5秒后,Android端会向Flutter端发送PUT请求';setState(() {});}).catchError((e) {if (e is MissingPluginException) {debugPrint('flutterSendAndroidDataNotice --- Error:notImplemented --- 未找到Android端具体实现函数');} else {debugPrint('flutterSendAndroidDataNotice --- Error:$e');}});}///  Flutter端 获取 Android端 数据,GET 操作flutterGetAndroidData() {// Android端调用Reply相关回调函数后,then、catchError 会接收到channel.send(null).then((value) {var androidCount = value['androidNum'];msgState = 'Android端接收Flutter端GET请求成功,数据:$androidCount ----> 5秒后,Android端会向Flutter端发送GET请求';setState(() {});}).catchError((e) {if (e is MissingPluginException) {debugPrint('flutterGetAndroidDataNotice --- Error:notImplemented --- 未找到Android端具体实现函数');} else {debugPrint('flutterGetAndroidDataNotice --- Error:$e');}});}/// 获取随机数int getRandomV() {return Random().nextInt(100); // 随机数范围(0-99)}@overrideWidget build(BuildContext context) {const defaultStyle = TextStyle(fontSize: 16,color: Colors.orangeAccent,fontWeight: FontWeight.bold,);return Scaffold(backgroundColor: Colors.blueGrey,body: SafeArea(top: true,child: SizedBox(width: MediaQuery.of(context).size.width,height: MediaQuery.of(context).size.height,child: Column(mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.center,children: [SizedBox(width: 300,child: Text(msgState,textAlign: TextAlign.center,style: defaultStyle)),Padding(padding: const EdgeInsets.symmetric(vertical: 16),child: ElevatedButton(onPressed: flutterSendAndroidData,child: const Text('Flutter端向Android端发送数据'),),),ElevatedButton(onPressed: flutterGetAndroidData,child: const Text('Flutter端获取Android端数据'),),],),),));}}

Android原生:TestJsonBasicMessageChannel.kt

package com.example.flutter_android_channel.channelimport android.os.Handler
import android.os.Looper
import android.util.Log
import io.flutter.plugin.common.BasicMessageChannel
import io.flutter.plugin.common.BinaryCodec
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.JSONMessageCodec
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.common.StringCodec
import org.json.JSONException
import org.json.JSONObject/*** BasicMessageChannel** 使用 JSONObject类型,对应 Flutter端的 Map类型*/
class TestJsonBasicMessageChannel(messenger: BinaryMessenger) :BasicMessageChannel.MessageHandler<Any> {private lateinit var mChannel: BasicMessageChannel<Any>companion object {// Android原生View 在Flutter引擎上注册的唯一标识,在Flutter端使用时必须一样private const val CHANNEL_NAME = "flutter.mix.android/json_basic_message_channel"}init {initChannel(messenger)}/*** 初始化消息通道*/private fun initChannel(messenger: BinaryMessenger) {// 创建 Android端和Flutter端的,相互通信的通道// 通道名称,两端必须一致mChannel = BasicMessageChannel(messenger, CHANNEL_NAME, JSONMessageCodec.INSTANCE)// 监听来自 Flutter端 的消息通道// Flutter端调用了函数,这个handler函数就会被触发mChannel.setMessageHandler(this)}// ========================== PUT 操作 ==========================/*** 监听来自 Flutter端 的消息通道** message: Android端 接收到 Flutter端 发来的 数据对象* reply:Android端 给 Flutter端 执行回调的接口对象*/override fun onMessage(message: Any?, reply: BasicMessageChannel.Reply<Any>) {// 回调结果对象// 获取Flutter端传过来的数据val flutterCount = getMap(message.toString())?.get("flutterNum")Log.d("TAG", "flutterCount:$flutterCount")// 回调状态接口对象,里面只有一个回调方法// reply.reply(@Nullable T reply)reply.reply(message) // 返回给Flutter端Handler(Looper.getMainLooper()).postDelayed({androidSendFlutterData()}, 5000)}/*** Android端 向 Flutter端 发送数据*/private fun androidSendFlutterData() {val map: MutableMap<String, Int> = mutableMapOf<String, Int>()map["androidNum"] = getRandomV() // 随机数范围(0-99)mChannel.send(map, object : BasicMessageChannel.Reply<Any> {override fun reply(reply: Any?) {// 获取Flutter端传过来的数据Log.d("TAG", "reply:$reply")}})}// ========================== GET 操作 ==========================//    /**
//     * 监听来自 Flutter端 的消息通道
//     *
//     * message: Android端 接收到 Flutter端 发来的 数据对象
//     * reply:Android端 给 Flutter端 执行回调的接口对象
//     */
//    override fun onMessage(message: Any?, reply: BasicMessageChannel.Reply<Any>) {
//        val map: MutableMap<String, Int> = mutableMapOf<String, Int>()
//        map["androidNum"] = getRandomV() // 随机数范围(0-99)
//        reply.reply(map) // 返回给Flutter端
//
//        Handler(Looper.getMainLooper()).postDelayed({
//            androidGetFlutterData()
//        }, 5000)
//    }
//
//    /**
//     * Android端 获取 Flutter端 数据
//     */
//    private fun androidGetFlutterData() {
//        mChannel.send(null, object : BasicMessageChannel.Reply<Any> {
//
//            override fun reply(reply: Any?) {
//                // 获取Flutter端传过来的数据
//                val flutterCount = getMap(reply.toString())?.get("flutterNum")
//                Log.d("TAG", "flutterCount:$flutterCount")
//            }
//
//        })
//    }/*** 获取随机数*/private fun getRandomV() = (0..100).random()/*** 解除绑定*/fun closeChannel() {mChannel.setMessageHandler(null)}/*** Json 转 Map*/private fun getMap(jsonString: String?): HashMap<String, Any>? {val jsonObject: JSONObjecttry {jsonObject = JSONObject(jsonString)val keyIter: Iterator<String> = jsonObject.keys()var key: Stringvar value: Anyvar valueMap = HashMap<String, Any>()while (keyIter.hasNext()) {key = keyIter.next()value = jsonObject[key] as AnyvalueMap[key] = value}return valueMap} catch (e: JSONException) {e.printStackTrace()}return null}
}

Android原生:MainActivity.kt

package com.example.flutter_android_channelimport com.example.flutter_android_channel.channel.TestJsonBasicMessageChannel
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngineclass MainActivity : FlutterActivity() {private lateinit var testJsonBasicMessageChannel: TestJsonBasicMessageChanneloverride fun configureFlutterEngine(flutterEngine: FlutterEngine) {super.configureFlutterEngine(flutterEngine)testJsonBasicMessageChannel = TestJsonBasicMessageChannel(flutterEngine.dartExecutor.binaryMessenger)}override fun onDestroy() {super.onDestroy()testJsonBasicMessageChannel.closeChannel()}}

1.6 传输BinaryCodec

Flutter:main_byte_basic_message_channel.dart


import 'package:just_audio/just_audio.dart';
import 'dart:typed_data';import 'package:flutter/material.dart';
import 'package:flutter/services.dart';void main() {runApp(const MyApp());
}class MyApp extends StatelessWidget {const MyApp({super.key});@overrideWidget build(BuildContext context) {return MaterialApp(debugShowCheckedModeBanner: false,title: 'Flutter Demo',theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),useMaterial3: true,),home: const MyHomePage(title: 'Flutter Demo Home Page'),);}
}/// BasicMessageChannel
/// 使用ByteData类型,对应 Android端的 ByteBuffer类型
class MyHomePage extends StatefulWidget {const MyHomePage({super.key, required this.title});final String title;@overrideState<MyHomePage> createState() => _MyHomePageState();
}class _MyHomePageState extends State<MyHomePage> {late BasicMessageChannel channel;// Android原生View 在Flutter引擎上注册的唯一标识,在Flutter端使用时必须一样static const String CHANNEL_NAME = 'flutter.mix.android/byte_basic_message_channel';String msgState = "默认"; // 消息传递状态@overrideinitState() {super.initState();initChannel();}/// 初始化消息通道initChannel() {channel = const BasicMessageChannel(CHANNEL_NAME,BinaryCodec()); // 创建 Flutter端和Android端的,相互通信的通道// 监听来自 Android端 的消息通道// Android端调用了函数,这个handler函数就会被触发channel.setMessageHandler(handler);}/// 监听来自 Android端 的消息通道/// Android端调用了函数,这个handler函数就会被触发Future<dynamic> handler(dynamic message) async {// PUTvar data = message as ByteData;loadMusic(data);msgState = 'Flutter端接收Android端PUT请求成功,音频加载完毕,开始播放';setState(() {});return ByteData.view(Uint8List(0).buffer); // 返回给Android端// GET,这里模拟在Android端播放音乐// final data = await rootBundle.load('assets/music/di_jia_a.mp3');// loadMusic(data);// msgState = 'Flutter端接收Android端GET请求成功,音频加载完毕,开始播放';// setState(() {});// return data; // 返回给Android端}/// Flutter端 向 Android端 发送数据,PUT 操作flutterSendAndroidData() async {final byteData = await rootBundle.load('assets/music/di_jia_a.mp3');// Android端调用Reply相关回调函数后,then、catchError 会接收到channel.send(byteData).then((value) {loadMusic(value);msgState = 'Android端接收Flutter端PUT请求成功,音频加载完毕,开始播放 --- 5秒钟后 Android端会向Flutter端发送PUT请求';setState(() {});}).catchError((e) {if (e is MissingPluginException) {debugPrint('flutterSendAndroidDataNotice --- Error:notImplemented --- 未找到Android端具体实现函数');} else {debugPrint('flutterSendAndroidDataNotice --- Error:$e');}});}///  Flutter端 获取 Android端 数据,GET 操作flutterGetAndroidData() {// Android端调用Reply相关回调函数后,then、catchError 会接收到channel.send(null).then((value) {loadMusic(value);msgState = 'Android端接收Flutter端GET请求成功,音频加载完毕,开始播放 --- 5秒钟后 Android端会向Flutter端发送GET请求';setState(() {});}).catchError((e) {if (e is MissingPluginException) {debugPrint('flutterGetAndroidDataNotice --- Error:notImplemented --- 未找到Android端具体实现函数');} else {debugPrint('flutterGetAndroidDataNotice --- Error:$e');}});}final player = AudioPlayer();/// 加载音频loadMusic(ByteData data) async {var buffer = data.buffer;var uint8list = buffer.asUint8List(data.offsetInBytes,data.lengthInBytes);var audioSource = AudioSource.uri(Uri.dataFromBytes(uint8list));await player.setAudioSource(audioSource);player.play(); // 播放音乐}/// 播放或暂停palsyOrPause() {if(player.playing) {player.pause();}}@overrideWidget build(BuildContext context) {const defaultStyle = TextStyle(fontSize: 16,color: Colors.orangeAccent,fontWeight: FontWeight.bold,);return Scaffold(backgroundColor: Colors.blueGrey,body: SafeArea(top: true,child: SizedBox(width: MediaQuery.of(context).size.width,height: MediaQuery.of(context).size.height,child: Column(mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.center,children: [SizedBox(width: 300,child: Text(msgState,textAlign: TextAlign.center,style: defaultStyle)),Padding(padding: const EdgeInsets.symmetric(vertical: 16),child: ElevatedButton(onPressed: flutterSendAndroidData,child: const Text('Flutter端向Android端发送数据'),),),Padding(padding: const EdgeInsets.only(bottom: 16),child: ElevatedButton(onPressed: flutterGetAndroidData,child: const Text('Flutter端获取Android端数据'),),),ElevatedButton(onPressed: palsyOrPause,child: Text('暂停'),),],),),));}}

Android原生:TestByteBasicMessageChannel.kt

package com.example.flutter_android_channel.channelimport android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.Log
import com.google.common.io.ByteStreams
import io.flutter.plugin.common.BasicMessageChannel
import io.flutter.plugin.common.BinaryCodec
import io.flutter.plugin.common.BinaryMessenger
import java.nio.ByteBuffer
import java.nio.ByteOrder/*** BasicMessageChannel** 使用 ByteBuffer类型,对应 Flutter端的 ByteData类型*/
class TestByteBasicMessageChannel(messenger: BinaryMessenger, private val context: Context) :BasicMessageChannel.MessageHandler<ByteBuffer> {private lateinit var mChannel: BasicMessageChannel<ByteBuffer>companion object {// Android原生View 在Flutter引擎上注册的唯一标识,在Flutter端使用时必须一样private const val CHANNEL_NAME = "flutter.mix.android/byte_basic_message_channel"}init {initChannel(messenger)}/*** 初始化消息通道*/private fun initChannel(messenger: BinaryMessenger) {// 创建 Android端和Flutter端的,相互通信的通道// 通道名称,两端必须一致mChannel = BasicMessageChannel(messenger, CHANNEL_NAME, BinaryCodec.INSTANCE)// 监听来自 Flutter端 的消息通道// Flutter端调用了函数,这个handler函数就会被触发mChannel.setMessageHandler(this)}// ========================== PUT 操作 ==========================/*** 监听来自 Flutter端 的消息通道** byteBuffer: Android端 接收到 Flutter端 发来的 数据对象* reply:Android端 给 Flutter端 执行回调的接口对象*/override fun onMessage(byteBuffer: ByteBuffer?, reply: BasicMessageChannel.Reply<ByteBuffer>) {// 回调结果对象// 获取Flutter端传过来的数据Log.d("TAG", "byteBuffer:$byteBuffer")// 回调接口对象,里面只有一个回调方法// reply.reply(@Nullable T reply)byteBuffer?.order(ByteOrder.nativeOrder())val direct = ByteBuffer.allocateDirect(byteBuffer!!.capacity())direct.put(byteBuffer)reply.reply(direct) // 返回给Flutter端Handler(Looper.getMainLooper()).postDelayed({androidSendFlutterData()}, 5000)}/*** Android端 向 Flutter端 发送数据*/private fun androidSendFlutterData() {// 读取assert目录下的音频文件val fileInputStream = context.assets.open("music/di_jia_b.mp3")val targetArray = ByteStreams.toByteArray(fileInputStream)val byteBuffer = ByteBuffer.wrap(targetArray)byteBuffer.order(ByteOrder.nativeOrder())val direct = ByteBuffer.allocateDirect(byteBuffer.capacity())direct.put(byteBuffer)mChannel.send(direct,object : BasicMessageChannel.Reply<ByteBuffer> {override fun reply(reply: ByteBuffer?) {Log.d("TAG", "reply:$reply")}})}// ========================== GET 操作 ==========================//    /**
//     * 监听来自 Flutter端 的消息通道
//     *
//     * byteBuffer: Android端 接收到 Flutter端 发来的 数据对象
//     * reply:Android端 给 Flutter端 执行回调的接口对象
//     */
//    override fun onMessage(byteBuffer: ByteBuffer?, reply: BasicMessageChannel.Reply<ByteBuffer>) {
//        // 读取assert目录下的音频文件
//        val fileInputStream = context.assets.open("music/di_jia_b.mp3")
//        val targetArray = ByteStreams.toByteArray(fileInputStream)
//        val byteBuffer = ByteBuffer.wrap(targetArray)
//
//        byteBuffer.order(ByteOrder.nativeOrder())
//        val direct = ByteBuffer.allocateDirect(byteBuffer.capacity())
//        direct.put(byteBuffer)
//        reply.reply(direct) // 返回给Flutter端
//
//        Handler(Looper.getMainLooper()).postDelayed({
//            androidGetFlutterData()
//        }, 5000)
//    }
//
//    /**
//     * Android端 获取 Flutter端 数据
//     */
//    private fun androidGetFlutterData() {
//        mChannel.send(null,object : BasicMessageChannel.Reply<ByteBuffer> {
//
//            override fun reply(reply: ByteBuffer?) {
//                // 获取Flutter端传过来的数据
//                Log.d("TAG", "reply:$reply")
//            }
//
//        })
//    }/*** 解除绑定*/fun closeChannel() {mChannel.setMessageHandler(null)}}

Android原生:MainActivity.kt

package com.example.flutter_android_channelimport com.example.flutter_android_channel.channel.TestByteBasicMessageChannel
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngineclass MainActivity : FlutterActivity() {private lateinit var testByteBasicMessageChannel: TestByteBasicMessageChanneloverride fun configureFlutterEngine(flutterEngine: FlutterEngine) {super.configureFlutterEngine(flutterEngine)        testByteBasicMessageChannel = TestByteBasicMessageChannel(flutterEngine.dartExecutor.binaryMessenger,this)        }override fun onDestroy() {super.onDestroy()testByteBasicMessageChannel.closeChannel()}}

2、MethodChannel

主要应用于Flutter和Android原生之间函数相互调用,所以它提供methodName作为具体调用函数的标识,就不用像演示 BasicMessageChannel那样,需要注释代码。

默认使用的消息类型是 StandardMessageCodec

Flutter:main_method_channel.dart

import 'dart:math';import 'package:flutter/material.dart';
import 'package:flutter/services.dart';void main() {runApp(const MyApp());
}class MyApp extends StatelessWidget {const MyApp({super.key});@overrideWidget build(BuildContext context) {return MaterialApp(debugShowCheckedModeBanner: false,title: 'Flutter Demo',theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),useMaterial3: true,),home: const MyHomePage(title: 'Flutter Demo Home Page'),);}
}/// MethodChannel
class MyHomePage extends StatefulWidget {const MyHomePage({super.key, required this.title});final String title;@overrideState<MyHomePage> createState() => _MyHomePageState();
}class _MyHomePageState extends State<MyHomePage> {late MethodChannel channel;// Android原生View 在Flutter引擎上注册的唯一标识,在Flutter端使用时必须一样static const String CHANNEL_NAME = 'flutter.mix.android/method_channel';static const String FLUTTER_SEND_ANDROID_DATA_NOTICE = 'flutterSendAndroidDataNotice'; // Flutter端 向 Android端 发送数据static const String FLUTTER_GET_ANDROID_DATA_NOTICE = 'flutterGetAndroidDataNotice'; // Flutter端 获取 Android端 数据static const String ANDROID_SEND_FLUTTER_DATA_NOTICE = 'androidSendFlutterDataNotice'; // Android端 向 Flutter端 发送数据static const String ANDROID_GET_FLUTTER_DATA_NOTICE = 'androidGetFlutterDataNotice'; // Android端 获取 Flutter端 数据String msgState = "默认"; // 消息传递状态@overrideinitState() {super.initState();initChannel();}/// 初始化消息通道initChannel() {channel = const MethodChannel(CHANNEL_NAME); // 创建 Flutter端和Android端的,相互通信的通道// 监听来自 Android端 的消息通道// Android端调用了函数,这个handler函数就会被触发channel.setMethodCallHandler(handler);}/// 监听来自 Android端 的消息通道/// Android端调用了函数,这个handler函数就会被触发Future<dynamic> handler(MethodCall call) async {// 获取调用函数的名称final String methodName = call.method;switch (methodName) {case ANDROID_SEND_FLUTTER_DATA_NOTICE:{int androidCount = call.arguments['androidNum'];msgState = 'Flutter端接收Android端PUT请求成功,数据:$androidCount';setState(() {});return '$ANDROID_SEND_FLUTTER_DATA_NOTICE ---> success'; // 返回给Android端}case ANDROID_GET_FLUTTER_DATA_NOTICE:{msgState = 'Flutter端接收Android端GET请求成功,返回数据:${getRandomV()}';setState(() {});return '$ANDROID_GET_FLUTTER_DATA_NOTICE ---> success:${getRandomV()}'; // 返回给Android端}default:{return PlatformException(code: '-1', message: '未找到Flutter端具体实现函数', details: '具体描述'); // 返回给Android端}}}/// Flutter端 向 Android端 发送数据,PUT 操作flutterSendAndroidData() {var randomV = getRandomV();Map<String, int> map = {'flutterNum': randomV};// Android端调用Result相关回调函数后,then、catchError 会接收到channel.invokeMethod(FLUTTER_SEND_ANDROID_DATA_NOTICE, map).then((value) {msgState = value;setState(() {});}).catchError((e) {if (e is MissingPluginException) {debugPrint('$FLUTTER_SEND_ANDROID_DATA_NOTICE --- Error:notImplemented --- 未找到Android端具体实现函数');} else {debugPrint('$FLUTTER_SEND_ANDROID_DATA_NOTICE --- Error:$e');}});}///  Flutter端 获取 Android端 数据,GET 操作flutterGetAndroidData() {// Android端调用Result相关回调函数后,then、catchError 会接收到channel.invokeMethod(FLUTTER_GET_ANDROID_DATA_NOTICE).then((value) {msgState = value;setState(() {});}).catchError((e) {if (e is MissingPluginException) {debugPrint('$FLUTTER_GET_ANDROID_DATA_NOTICE --- Error:notImplemented --- 未找到Android端具体实现函数');} else {debugPrint('$FLUTTER_GET_ANDROID_DATA_NOTICE --- Error:$e');}});}/// 获取随机数int getRandomV() {return Random().nextInt(100); // 随机数范围(0-99)}@overrideWidget build(BuildContext context) {const defaultStyle = TextStyle(fontSize: 16,color: Colors.orangeAccent,fontWeight: FontWeight.bold,);return Scaffold(backgroundColor: Colors.blueGrey,body: SafeArea(top: true,child: SizedBox(width: MediaQuery.of(context).size.width,height: MediaQuery.of(context).size.height,child: Column(mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.center,children: [SizedBox(width: 300,child: Text(msgState,textAlign: TextAlign.center,style: defaultStyle)),Padding(padding: const EdgeInsets.symmetric(vertical: 16),child: ElevatedButton(onPressed: flutterSendAndroidData,child: const Text('Flutter端向Android端发送数据'),),),ElevatedButton(onPressed: flutterGetAndroidData,child: const Text('Flutter端获取Android端数据'),),],),),));}}

Android原生:TestMethodChannel.kt

package com.example.flutter_android_channel.channelimport android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.annotation.NonNull
import androidx.annotation.Nullable
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import kotlin.random.Random/*** MethodChannel*/
class TestMethodChannel(messenger: BinaryMessenger) : MethodChannel.MethodCallHandler {private lateinit var mChannel: MethodChannelcompanion object {// Android原生View 在Flutter引擎上注册的唯一标识,在Flutter端使用时必须一样private const val CHANNEL_NAME = "flutter.mix.android/method_channel"private const val ANDROID_SEND_FLUTTER_DATA_NOTICE: String = "androidSendFlutterDataNotice" // Android端 向 Flutter端 发送数据private const val ANDROID_GET_FLUTTER_DATA_NOTICE: String = "androidGetFlutterDataNotice" // Android端 获取 Flutter端 数据private const val FLUTTER_SEND_ANDROID_DATA_NOTICE: String = "flutterSendAndroidDataNotice" // Flutter端 向 Android端 发送数据private const val FLUTTER_GET_ANDROID_DATA_NOTICE: String = "flutterGetAndroidDataNotice" // Flutter端 获取 Android端 数据}init {initChannel(messenger)}/*** 初始化消息通道*/private fun initChannel(messenger: BinaryMessenger) {// 创建 Android端和Flutter端的,相互通信的通道// 通道名称,两端必须一致mChannel = MethodChannel(messenger, CHANNEL_NAME)// 监听来自 Flutter端 的消息通道// Flutter端调用了函数,这个handler函数就会被触发mChannel.setMethodCallHandler(this)}/*** 监听来自 Flutter端 的消息通道** call: Android端 接收到 Flutter端 发来的 数据对象* result:Android端 给 Flutter端 执行回调的接口对象*/override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {// 获取调用函数的名称val methodName: String = call.methodwhen (methodName) {FLUTTER_SEND_ANDROID_DATA_NOTICE -> {// 回调结果对象// 获取Flutter端传过来的数据val flutterCount: Int? = call.argument<Int>("flutterNum")result.success("Android端接收Flutter端PUT请求成功,数据:$flutterCount ----> 5秒后,Android端会向Flutter端发送PUT请求")Handler(Looper.getMainLooper()).postDelayed({androidSendFlutterData()}, 5000)// 回调状态接口对象,里面有三个回调方法,都可以给Flutter端返回消息// result.success(result: Any?)// result.error(errorCode: String, errorMessage: String?, errorDetails: Any?)// result.notImplemented()}FLUTTER_GET_ANDROID_DATA_NOTICE -> {result.success("Android端接收Flutter端GET请求成功,返回数据:${getRandomV()} ----> 5秒后,Android端会向Flutter端发送GET请求")Handler(Looper.getMainLooper()).postDelayed({androidGetFlutterData()}, 5000)}else -> {result.notImplemented()}}}/*** Android端 向 Flutter端 发送数据,相当于 PUT 操作*/private fun androidSendFlutterData() {val map: MutableMap<String, Int> = mutableMapOf<String, Int>()map["androidNum"] = getRandomV() // 随机数范围(0-99)mChannel.invokeMethod(ANDROID_SEND_FLUTTER_DATA_NOTICE, map, object : MethodChannel.Result {override fun success(result: Any?) {Log.d("TAG", "$result")}override fun error(errorCode: String,errorMessage: String?,errorDetails: Any?) {Log.d("TAG", "errorCode:$errorCode --- errorMessage:$errorMessage --- errorDetails:$errorDetails")}/*** Flutter端 未实现 Android端 定义的接口方法*/override fun notImplemented() {Log.d("TAG", "notImplemented")}})}/*** Android端 获取 Flutter端 数据,相当于 GET 操作*/private fun androidGetFlutterData() {// 说一个坑,不传参数可以写null,// 但不能这样写,目前它没有这个重载方法,invokeMethod第二个参数是Object类型,所以编译器不会提示错误// mChannel.invokeMethod(ANDROID_GET_FLUTTER_DATA_NOTICE, object : MethodChannel.Result {// public void invokeMethod(@NonNull String method, @Nullable Object arguments)mChannel.invokeMethod(ANDROID_GET_FLUTTER_DATA_NOTICE, null, object : MethodChannel.Result {override fun success(result: Any?) {Log.d("TAG", "$result")}override fun error(errorCode: String,errorMessage: String?,errorDetails: Any?) {Log.d("TAG", "errorCode:$errorCode --- errorMessage:$errorMessage --- errorDetails:$errorDetails")}/*** Flutter端 未实现 Android端 定义的接口方法*/override fun notImplemented() {Log.d("TAG", "notImplemented")}})}/*** 获取随机数*/private fun getRandomV() = (0..100).random()/*** 解除绑定*/fun closeChannel() {mChannel.setMethodCallHandler(null)}}

Android原生:MainActivity.kt

package com.example.flutter_android_channelimport com.example.flutter_android_channel.channel.TestMethodChannel
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngineclass MainActivity : FlutterActivity() {TestByteBasicMessageChannelprivate lateinit var testMethodChannel: TestMethodChanneloverride fun configureFlutterEngine(flutterEngine: FlutterEngine) {super.configureFlutterEngine(flutterEngine)testMethodChannel = TestMethodChannel(flutterEngine.dartExecutor.binaryMessenger)}override fun onDestroy() {super.onDestroy()testMethodChannel.closeChannel()}}

3、EventChannel

主要应用于 原生端 向 Flutter 单向通信

默认使用的消息类型是 StandardMessageCodec

Flutter:main_single_event_channel.dart


import 'dart:async';import 'package:flutter/material.dart';
import 'package:flutter/services.dart';void main() {runApp(const MyApp());
}class MyApp extends StatelessWidget {const MyApp({super.key});@overrideWidget build(BuildContext context) {return MaterialApp(debugShowCheckedModeBanner: false,title: 'Flutter Demo',theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),useMaterial3: true,),home: const MyHomePage(title: 'Flutter Demo Home Page'),);}
}/// EventChannel
/// 使用方式:单向通信
class MyHomePage extends StatefulWidget {const MyHomePage({super.key, required this.title});final String title;@overrideState<MyHomePage> createState() => _MyHomePageState();
}class _MyHomePageState extends State<MyHomePage> {late EventChannel channel;// Android原生View 在Flutter引擎上注册的唯一标识,在Flutter端使用时必须一样static const String CHANNEL_NAME = 'flutter.mix.android/single_event_channel';StreamSubscription? streamSubscription;String msgState = "默认"; // 消息传递状态@overrideinitState() {super.initState();initChannel();}/// 初始化消息通道initChannel() {channel = const EventChannel(CHANNEL_NAME); // 创建 Flutter端和Android端的,相互通信的通道// 监听来自 Android端 的消息通道// Android端调用了函数,这个handler函数就会被触发streamSubscription = channel.receiveBroadcastStream().listen(onData, onError: onError, onDone: onDone);}/// 监听来自 Android端 的消息通道/// 这几个函数就会根据情况被触发/// 响应数据onData(dynamic data) {msgState = data;setState(() {});}/// 发生异常onError(dynamic error) {msgState = error;setState(() {});}/// 流被关闭onDone() {msgState = "流被关闭";setState(() {});}@overridevoid dispose() {super.dispose();streamSubscription?.cancel();}@overrideWidget build(BuildContext context) {const defaultStyle = TextStyle(fontSize: 50,color: Colors.orangeAccent,fontWeight: FontWeight.bold,);return Scaffold(backgroundColor: Colors.blueGrey,body: SafeArea(top: true,child: SizedBox(width: MediaQuery.of(context).size.width,height: MediaQuery.of(context).size.height,child: Column(mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.center,children: [SizedBox(width: 300,child: Text(msgState,textAlign: TextAlign.center, style: defaultStyle)),],),),));}
}

Android原生:TestSingleEventChannel.kt

package com.example.flutter_android_channel.channelimport android.os.CountDownTimer
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.EventChannel/*** EventChannel** 使用方式:单向通信*/
class TestSingleEventChannel(messenger: BinaryMessenger) : EventChannel.StreamHandler {private lateinit var mChannel: EventChannelcompanion object {// Android原生View 在Flutter引擎上注册的唯一标识,在Flutter端使用时必须一样private const val CHANNEL_NAME = "flutter.mix.android/single_event_channel"}init {initChannel(messenger)}/*** 初始化消息通道*/private fun initChannel(messenger: BinaryMessenger) {// 创建 Android端和Flutter端的,相互通信的通道// 通道名称,两端必须一致mChannel = EventChannel(messenger, CHANNEL_NAME)// 监听来自 Flutter端 的消息通道// Flutter端调用了函数,这个handler函数就会被触发mChannel.setStreamHandler(this)}private var count: Int = 10override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {// 一共10秒,每隔1秒执行一次object : CountDownTimer(10000, 1000) {override fun onTick(millisUntilFinished: Long) {// 还剩下多少秒,依次为2000、1000、0if (millisUntilFinished == 0L) {cancel()}events?.success("${count--}")}override fun onFinish() { // 结束后的操作events?.success("${count--}")}}.start()// 给Flutter端返回消息// events?.endOfStream()// events?.success(event: Any?)// events?.error(errorCode: String?, errorMessage: String?, errorDetails: Any?)// events?.endOfStream() // 流结束}override fun onCancel(arguments: Any?) {}/*** 解除绑定*/fun closeChannel() {mChannel.setStreamHandler(null)}}

Android原生:MainActivity.kt

package com.example.flutter_android_channelimport com.example.flutter_android_channel.channel.TestSingleEventChannel
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngineclass MainActivity : FlutterActivity() {private lateinit var testSingleEventChannel : TestSingleEventChanneloverride fun configureFlutterEngine(flutterEngine: FlutterEngine) {super.configureFlutterEngine(flutterEngine)testSingleEventChannel = TestSingleEventChannel(flutterEngine.dartExecutor.binaryMessenger)}override fun onDestroy() {super.onDestroy()testSingleEventChannel.closeChannel()}}

怀疑

  • 它用到Stream,流在开发中,代表一连串数据,主要应用在传输文件,比如IO流;
  • EventChannel.EventSink,它提供的那些函数名,一看就是返回操作,既然是返回,就需要前置条件,比如接收到请求;
  • BinaryMessenger.send(String channel, ByteData? message),我找到了方法,传输的数据类型是ByteData,结合前两项,逻辑通了,那是否意味着Flutter可以使用EventChannel主动向原生端发送请求

1.0 撸起袖子,开始干

Flutter一切正常,成功发送数据,且Android原生也成功接收到数据,但遇到了解析异常;

17:53:44.737  E  Uncaught exception in binary message listenerjava.lang.IllegalArgumentException: Message corruptedat io.flutter.plugin.common.StandardMessageCodec.readValueOfType(StandardMessageCodec.java:450)at io.flutter.plugin.common.StandardMessageCodec.readValue(StandardMessageCodec.java:340)at io.flutter.plugin.common.StandardMethodCodec.decodeMethodCall(StandardMethodCodec.java:48)at io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler.onMessage(EventChannel.java:195)at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)at android.os.Handler.handleCallback(Handler.java:942)at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loopOnce(Looper.java:201)at android.os.Looper.loop(Looper.java:288)at android.app.ActivityThread.main(ActivityThread.java:7872)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

1.1 异常分析

这个异常是在原生端触发的,默认使用的消息类型是 StandardMessageCodec,它的父类是MethodCodec抽象类,目前没有可以解析 ByteBuffer的子类,这就很矛盾,发送成功,接收成功,但不支持解析,于是我抱着侥幸的心态,跑去问官方,看看是不是我使用的方式不对。

StandardMessageCodec.java 中的数据解析方法;

 @Nullableprotected Object readValueOfType(byte type, @NonNull ByteBuffer buffer) {final Object result;switch (type) {case NULL:result = null;break;case TRUE:result = true;break;case FALSE:result = false;break;case INT:result = buffer.getInt();break;case LONG:result = buffer.getLong();break;case BIGINT:{final byte[] hex = readBytes(buffer);result = new BigInteger(new String(hex, UTF8), 16);break;}case DOUBLE:readAlignment(buffer, 8);result = buffer.getDouble();break;case STRING:{final byte[] bytes = readBytes(buffer);result = new String(bytes, UTF8);break;}case BYTE_ARRAY:{result = readBytes(buffer);break;}case INT_ARRAY:{final int length = readSize(buffer);final int[] array = new int[length];readAlignment(buffer, 4);buffer.asIntBuffer().get(array);result = array;buffer.position(buffer.position() + 4 * length);break;}case LONG_ARRAY:{final int length = readSize(buffer);final long[] array = new long[length];readAlignment(buffer, 8);buffer.asLongBuffer().get(array);result = array;buffer.position(buffer.position() + 8 * length);break;}case DOUBLE_ARRAY:{final int length = readSize(buffer);final double[] array = new double[length];readAlignment(buffer, 8);buffer.asDoubleBuffer().get(array);result = array;buffer.position(buffer.position() + 8 * length);break;}case LIST:{final int size = readSize(buffer);final List<Object> list = new ArrayList<>(size);for (int i = 0; i < size; i++) {list.add(readValue(buffer));}result = list;break;}case MAP:{final int size = readSize(buffer);final Map<Object, Object> map = new HashMap<>();for (int i = 0; i < size; i++) {map.put(readValue(buffer), readValue(buffer));}result = map;break;}case FLOAT_ARRAY:{final int length = readSize(buffer);final float[] array = new float[length];readAlignment(buffer, 4);buffer.asFloatBuffer().get(array);result = array;buffer.position(buffer.position() + 4 * length);break;}default:throw new IllegalArgumentException("Message corrupted");}return result;}

1.2 Flutter官方回复

这是Git问题地址:https://github.com/flutter/flutter/issues/141876

这是回复,我翻译成中文。

问题:那么EventChannel只能用于与flutter进行原生单向通信吗?

答复:是的。

问题:如果是这样,为什么Flutter EventChannel提供了channel.binaryMessenger.send方法和channel.binaryMessenger.setMessageHandler监听

答复:事实并非如此; 这些是 BinaryMessenger 上的方法。 BinaryMessenger 是 EventChannel 构建的较低级别构造,这就是它采用二进制信使作为构造函数参数的原因。

问题:还是我的使用方式有问题?

答复:是的。 您试图绕过 EventChannel 抽象并发送您自己的原始二进制消息,这些消息不符合 EventChannel 使用二进制消息传递程序的方式,因此您收到的崩溃是预期的结果。

如果您想要发送自己的原始二进制消息,那么您需要使用自己的通道,而不是 EventChannel。

好了,不用纠结了,官方实锤 EventChannel 只能由 原生端 向 Flutter 单向通信

别的不说,这官方回复问题的效率是真高。

Flutter:main_bilateral_event_channel.dart


import 'dart:async';import 'package:flutter/material.dart';
import 'package:flutter/services.dart';void main() {runApp(const MyApp());
}class MyApp extends StatelessWidget {const MyApp({super.key});@overrideWidget build(BuildContext context) {return MaterialApp(debugShowCheckedModeBanner: false,title: 'Flutter Demo',theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),useMaterial3: true,),home: const MyHomePage(title: 'Flutter Demo Home Page'),);}
}/// EventChannel
/// 使用方式:尝试双向通信,但失败
class MyHomePage extends StatefulWidget {const MyHomePage({super.key, required this.title});final String title;@overrideState<MyHomePage> createState() => _MyHomePageState();
}class _MyHomePageState extends State<MyHomePage> {late EventChannel channel;// Android原生View 在Flutter引擎上注册的唯一标识,在Flutter端使用时必须一样static const String CHANNEL_NAME = 'flutter.mix.android/bilateral_event_channel';StreamSubscription? streamSubscription;String msgState = "默认"; // 消息传递状态@overrideinitState() {super.initState();initChannel();}/// 初始化消息通道initChannel() {// channel = EventChannel(CHANNEL_NAME, CustomizeStandardMethodCodec());channel = const EventChannel(CHANNEL_NAME); // 创建 Flutter端和Android端的,相互通信的通道// 监听来自 Android端 的消息通道// Android端调用了函数,这个handler函数就会被触发streamSubscription = channel.receiveBroadcastStream().listen(onData, onError: onError, onDone: onDone);}/// 监听来自 Android端 的消息通道/// 这几个函数就会根据情况被触发/// 响应数据onData(dynamic data) {msgState = data;setState(() {});}/// 发生异常onError(dynamic error) {msgState = error;setState(() {});}/// 流被关闭onDone() {msgState = "流被关闭";setState(() {});}/// Flutter端 向 Android端 发送数据,相当于 PUT 操作flutterSendAndroidData() async {final byteData = await rootBundle.load('assets/music/di_jia_a.mp3');channel.binaryMessenger.send(CHANNEL_NAME, byteData);}@overridevoid dispose() {super.dispose();streamSubscription?.cancel();}@overrideWidget build(BuildContext context) {const defaultStyle = TextStyle(fontSize: 30,color: Colors.orangeAccent,fontWeight: FontWeight.bold,);return Scaffold(backgroundColor: Colors.blueGrey,body: SafeArea(top: true,child: SizedBox(width: MediaQuery.of(context).size.width,height: MediaQuery.of(context).size.height,child: Column(mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.center,children: [Padding(padding: const EdgeInsets.only(bottom: 16),child: SizedBox(width: 300,child: Text(msgState,textAlign: TextAlign.center, style: defaultStyle)),),ElevatedButton(onPressed: flutterSendAndroidData,child: const Text('发送',style: TextStyle(fontSize: 20)),)],),),));}
}

Android原生:TestBilateralEventChannel.kt

package com.example.flutter_android_channel.channelimport android.util.Log
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.EventChannel/*** EventChannel** 使用方式:尝试双向通信,但失败*/
class TestBilateralEventChannel(messenger: BinaryMessenger) : EventChannel.StreamHandler {private lateinit var mChannel: EventChannelcompanion object {// Android原生View 在Flutter引擎上注册的唯一标识,在Flutter端使用时必须一样private const val CHANNEL_NAME = "flutter.mix.android/bilateral_event_channel"}init {initChannel(messenger)}/*** 初始化消息通道*/private fun initChannel(messenger: BinaryMessenger) {// 创建 Android端和Flutter端的,相互通信的通道// 通道名称,两端必须一致mChannel = EventChannel(messenger, CHANNEL_NAME)// 监听来自 Flutter端 的消息通道// Flutter端调用了函数,这个handler函数就会被触发mChannel.setStreamHandler(this)}override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {Log.d("TAG","arguments:$arguments")}override fun onCancel(arguments: Any?) {}/*** 解除绑定*/fun closeChannel() {mChannel.setStreamHandler(null)}}

Android原生:MainActivity.kt

package com.example.flutter_android_channelimport com.example.flutter_android_channel.channel.TestBilateralEventChannel
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngineclass MainActivity : FlutterActivity() {private lateinit var testBilateralEventChannel: TestBilateralEventChanneloverride fun configureFlutterEngine(flutterEngine: FlutterEngine) {super.configureFlutterEngine(flutterEngine)testBilateralEventChannel = TestBilateralEventChannel(flutterEngine.dartExecutor.binaryMessenger)}override fun onDestroy() {super.onDestroy()testBilateralEventChannel.closeChannel()}}

4、不同的Channel配合使用

不同的Channel针对不同的应用场景,实际开发中,根据业务需求会相互配合使用;

BasicMessageChannel 和 MethodChannel 一起配合使用,使用BasicMessageChannel 传输数据,传输完成后,调用 MethodChannel 向Flutter发送消息。

Flutter:main_mix_use_channel.dart

import 'dart:math';import 'package:flutter/material.dart';
import 'package:flutter/services.dart';void main() {runApp(const MyApp());
}class MyApp extends StatelessWidget {const MyApp({super.key});@overrideWidget build(BuildContext context) {return MaterialApp(debugShowCheckedModeBanner: false,title: 'Flutter Demo',theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),useMaterial3: true,),home: const MyHomePage(title: 'Flutter Demo Home Page'),);}
}/// BasicMessageChannel + MethodChannel 一起配合使用
class MyHomePage extends StatefulWidget {const MyHomePage({super.key, required this.title});final String title;@overrideState<MyHomePage> createState() => _MyHomePageState();
}class _MyHomePageState extends State<MyHomePage> {late BasicMessageChannel mBasicMessageChannel;late MethodChannel mMethodChannel;// Android原生View 在Flutter引擎上注册的唯一标识,在Flutter端使用时必须一样static const String MIX_BASIC_MESSAGE_CHANNEL_NAME = 'flutter.mix.android/mix_json_basic_message_channel';static const String MIX_METHOD_CHANNEL_NAME = 'flutter.mix.android/mix_method_channel';static const String ANDROID_SEND_FLUTTER_DATA_NOTICE = 'androidSendFlutterDataNotice'; // Android端 向 Flutter端 发送数据String msgState = "默认"; // 消息传递状态@overrideinitState() {super.initState();initChannel();}/// 初始化消息通道initChannel() {// 创建 Flutter端和Android端的,相互通信的通道mBasicMessageChannel = const BasicMessageChannel(MIX_BASIC_MESSAGE_CHANNEL_NAME,JSONMessageCodec());mMethodChannel = const MethodChannel(MIX_METHOD_CHANNEL_NAME);// 监听来自 Android端 的消息通道// Android端调用了函数,这个handler函数就会被触发mBasicMessageChannel.setMessageHandler(messageHandler);mMethodChannel.setMethodCallHandler(methodHandler);}/// 监听来自 Android端 的 BasicMessageChannel 消息通道/// Android端调用了函数,这个handler函数就会被触发Future<dynamic> messageHandler(dynamic message) async {}/// 监听来自 Android端 的 MethodChannel 消息通道/// Android端调用了函数,这个handler函数就会被触发Future<dynamic> methodHandler(MethodCall call) async {// 获取调用函数的名称final String methodName = call.method;switch (methodName) {case ANDROID_SEND_FLUTTER_DATA_NOTICE:{int androidCount = call.arguments['androidNum'];msgState = 'Flutter端接收Android端请求成功,数据:$androidCount';setState(() {});return '$ANDROID_SEND_FLUTTER_DATA_NOTICE ---> success'; // 返回给Android端}default:{return PlatformException(code: '-1', message: '未找到Flutter端具体实现函数', details: '具体描述'); // 返回给Android端}}}/// Flutter端 向 Android端 发送数据,PUT 操作flutterSendAndroidData() {var randomV = getRandomV();Map<String, int> map = {'flutterNum': randomV};// Android端调用Reply相关回调函数后,then、catchError 会接收到mBasicMessageChannel.send(map).then((value) {var flutterNum = value['flutterNum'];msgState = 'Android端接收Flutter端请求成功,数据:$flutterNum ----> 5秒后,Android端会向Flutter端发送请求';setState(() {});}).catchError((e) {if (e is MissingPluginException) {debugPrint('flutterSendAndroidDataNotice --- Error:notImplemented --- 未找到Android端具体实现函数');} else {debugPrint('flutterSendAndroidDataNotice --- Error:$e');}});}/// 获取随机数int getRandomV() {return Random().nextInt(100); // 随机数范围(0-99)}@overrideWidget build(BuildContext context) {const defaultStyle = TextStyle(fontSize: 16,color: Colors.orangeAccent,fontWeight: FontWeight.bold,);return Scaffold(backgroundColor: Colors.blueGrey,body: SafeArea(top: true,child: SizedBox(width: MediaQuery.of(context).size.width,height: MediaQuery.of(context).size.height,child: Column(mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.center,children: [SizedBox(width: 300,child: Text(msgState,textAlign: TextAlign.center,style: defaultStyle)),Padding(padding: const EdgeInsets.symmetric(vertical: 16),child: ElevatedButton(onPressed: flutterSendAndroidData,child: const Text('Flutter端向Android端发送数据'),),)],),),));}}

Android原生:TestMixUseChannel.kt

package com.example.flutter_android_channel.channelimport android.os.Handler
import android.os.Looper
import android.util.Log
import io.flutter.plugin.common.BasicMessageChannel
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.JSONMessageCodec
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import org.json.JSONException
import org.json.JSONObject/*** BasicMessageChannel + MethodChannel 一起配合使用**/
class TestMixUseChannel(messenger: BinaryMessenger) : BasicMessageChannel.MessageHandler<Any>,MethodChannel.MethodCallHandler {private lateinit var mBasicMessageChannel: BasicMessageChannel<Any>private lateinit var mMethodChannel: MethodChannelcompanion object {// Android原生View 在Flutter引擎上注册的唯一标识,在Flutter端使用时必须一样private const val MIX_BASIC_MESSAGE_CHANNEL_NAME = "flutter.mix.android/mix_json_basic_message_channel"private const val MIX_METHOD_CHANNEL_NAME = "flutter.mix.android/mix_method_channel"private const val ANDROID_SEND_FLUTTER_DATA_NOTICE: String = "androidSendFlutterDataNotice" // Android端 向 Flutter端 发送数据}init {initChannel(messenger)}/*** 初始化消息通道*/private fun initChannel(messenger: BinaryMessenger) {// 创建 Android端和Flutter端的,相互通信的通道// 通道名称,两端必须一致mBasicMessageChannel = BasicMessageChannel(messenger, MIX_BASIC_MESSAGE_CHANNEL_NAME, JSONMessageCodec.INSTANCE)mMethodChannel = MethodChannel(messenger, MIX_METHOD_CHANNEL_NAME)// 监听来自 Flutter端 的消息通道// Flutter端调用了函数,这个handler函数就会被触发mBasicMessageChannel.setMessageHandler(this)mMethodChannel.setMethodCallHandler(this)}/*** 监听来自 Flutter端 的 BasicMessageChannel 消息通道** message: Android端 接收到 Flutter端 发来的 数据对象* reply:Android端 给 Flutter端 执行回调的接口对象*/override fun onMessage(message: Any?, reply: BasicMessageChannel.Reply<Any>) {// 回调结果对象// 获取Flutter端传过来的数据val flutterCount = getMap(message.toString())?.get("flutterNum")Log.d("TAG", "flutterCount:$flutterCount")// 回调状态接口对象,里面只有一个回调方法// reply.reply(@Nullable T reply)reply.reply(message) // 返回给Flutter端Handler(Looper.getMainLooper()).postDelayed({androidSendFlutterData()}, 5000)}/*** 监听来自 Flutter端 的 MethodChannel 消息通道** call: Android端 接收到 Flutter端 发来的 数据对象* result:Android端 给 Flutter端 执行回调的接口对象*/override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {}/*** Android端 向 Flutter端 发送数据,相当于 PUT 操作*/private fun androidSendFlutterData() {val map: MutableMap<String, Int> = mutableMapOf<String, Int>()map["androidNum"] = getRandomV() // 随机数范围(0-99)mMethodChannel.invokeMethod(ANDROID_SEND_FLUTTER_DATA_NOTICE, map, object : MethodChannel.Result {override fun success(result: Any?) {Log.d("TAG", "$result")}override fun error(errorCode: String,errorMessage: String?,errorDetails: Any?) {Log.d("TAG", "errorCode:$errorCode --- errorMessage:$errorMessage --- errorDetails:$errorDetails")}/*** Flutter端 未实现 Android端 定义的接口方法*/override fun notImplemented() {Log.d("TAG", "notImplemented")}})}/*** 解除绑定*/fun closeChannel() {mBasicMessageChannel.setMessageHandler(null)mMethodChannel.setMethodCallHandler(null)}/*** 获取随机数*/private fun getRandomV() = (0..100).random()/*** Json 转 Map*/private fun getMap(jsonString: String?): HashMap<String, Any>? {val jsonObject: JSONObjecttry {jsonObject = JSONObject(jsonString)val keyIter: Iterator<String> = jsonObject.keys()var key: Stringvar value: Anyvar valueMap = HashMap<String, Any>()while (keyIter.hasNext()) {key = keyIter.next()value = jsonObject[key] as AnyvalueMap[key] = value}return valueMap} catch (e: JSONException) {e.printStackTrace()}return null}}

Android原生:MainActivity.kt

package com.example.flutter_android_channelimport com.example.flutter_android_channel.channel.TestMixUseChannelimport io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngineclass MainActivity : FlutterActivity() {private lateinit var testMixUseChannel: TestMixUseChanneloverride fun configureFlutterEngine(flutterEngine: FlutterEngine) {super.configureFlutterEngine(flutterEngine)testMixUseChannel = TestMixUseChannel(flutterEngine.dartExecutor.binaryMessenger)}override fun onDestroy() {super.onDestroy()testMixUseChannel.closeChannel()}}

5、执行线程

都试了一遍,目前这些Channel都只能在主线程执行。

// Android原生// 测试在子线程执行
Thread {androidSendFlutterData()
}.start()
Process: com.example.flutter_android_channel, PID: 5776java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: Thread-4at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:1496)at io.flutter.embedding.engine.FlutterJNI.dispatchPlatformMessage(FlutterJNI.java:1103)at io.flutter.embedding.engine.dart.DartMessenger.send(DartMessenger.java:282)at io.flutter.embedding.engine.dart.DartExecutor$DefaultBinaryMessenger.send(DartExecutor.java:470)at io.flutter.plugin.common.BasicMessageChannel.send(BasicMessageChannel.java:105)at com.example.flutter_android_channel.channel.TestJsonBasicMessageChannel.androidSendFlutterData(TestJsonBasicMessageChannel.kt:80)at com.example.flutter_android_channel.channel.TestJsonBasicMessageChannel.onMessage$lambda-1$lambda-0(TestJsonBasicMessageChannel.kt:68)at com.example.flutter_android_channel.channel.TestJsonBasicMessageChannel.$r8$lambda$vHbhJxL-HBJ37W1nuB2sJQfndKs(Unknown Source:0)at com.example.flutter_android_channel.channel.TestJsonBasicMessageChannel$$ExternalSyntheticLambda1.run(Unknown Source:2)

6、总结

BasicMessageChannel主要应用于:传输数据;

MethodChannel主要应用于:通过函数处理业务逻辑;

EventChannel主要应用于:一些只能由原生端API才能完成的操作,处理完后发送给Flutter;

双向通信:BasicMessageChannelMethodChannel

单向通信:EventChannel

7、源码地址

https://github.com/LanSeLianMa/flutter_android_channel

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/418874.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

第二课:BERT

文章目录 第二课&#xff1a;BERT1、学习总结&#xff1a;为什么要学习BERT&#xff1f;预训练模型的发展历程BERT结构BERT 输入BERT EmbeddingBERT 模型构建BERT self-attention 层BERT self-attention 输出层BERT feed-forward 层BERT 最后的Add&NormBERT EncoderBERT 输…

BlueBunny:基于低功耗蓝牙的Bash bunny命令控制C2框架

关于BlueBunny BlueBunny是一款功能强大的命令控制框架&#xff0c;该工具基于低功耗蓝牙实现数据通信&#xff0c;可以帮助广大研究人员直接通过蓝牙将控制指令发送给Bash Bunny。 什么是Bash Bunny Bash Bunny是一款类似于USB Rubber Ducky的多功能USB攻击工具&#xff08;…

Mysql 编译安装部署

Mysql 编译安装部署 环境&#xff1a; 172.20.26.198&#xff08;Centos7.6&#xff09; 源码安装Mysql-5.7 大概步骤如下&#xff1a; 1、上传mysql-5.7.28.tar.gz 、boost_1_59_0.tar 到/usr/src 目录下 2、安装依赖 3、cmake 4、make && make install 5、…

MyBatis 系列:MyBatis 源码环境搭建

文章目录 一、环境准备二、下载 MyBatis 源码和 MyBatis-Parent 源码三、创建空项目、导入项目四、编译 mybatis-parent五、编译 mybatis六、测试总结 一、环境准备 jdk&#xff1a;17 maven&#xff1a;3.9.5 二、下载 MyBatis 源码和 MyBatis-Parent 源码 Mybatis&#x…

外呼机器人有什么优势?

外呼机器人有什么优势&#xff1f;值得受到大多数电销企业的追捧&#xff01; 1、电话外呼效率高&#xff1a; 每天可拨打的电话数量是人工的5-10倍&#xff0c;人工一天只能拨打200-300通电话&#xff0c;机器人每天能打3000通电话以上&#xff0c;无须休息&#xff0c;按照…

数学建模实战Matlab绘图

二维曲线、散点图 绘图命令&#xff1a;plot(x,y,’line specifiers’,’PropertyName’,PropertyValue) 例子&#xff1a;绘图表示年收入与年份的关系 ‘--r*’:--设置线型&#xff1b;r:设置颜色为红色&#xff1b;*节点型号 ‘linewidth’&#xff1a;设置线宽&#xff1…

el-upload中的before-upload不生效

我们先来看看官方对before-upload的定义 before-upload是在上传文件时触发&#xff0c;不是添加文件时触发&#xff0c;添加文件时触发 on-change。 所以如果我们要在添加文件时&#xff0c;对文件的大小和后缀等等进行判断&#xff0c;可以用 on-change 方法来实现。 checkSu…

云原生场景下,AIGC 模型服务的工程挑战和应对

作者&#xff1a;徐之浩、车漾 “成本”、“性能”和 “效率”正在成为影响大模型生产和应用的三个核心因素&#xff0c;也是企业基础设施在面临生产、使用大模型时的全新挑战。AI 领域的快速发展不仅需要算法的突破&#xff0c;也需要工程的创新。 大模型推理对基础设施带来…

MSPM0L1306例程学习-UART部分(2)

MSPM0L1306例程学习系列 1.背景介绍 写在前边的话&#xff1a; 这个系列比较简单&#xff0c;主要是围绕TI官网给出的SDK例程进行讲解和注释。并没有针对模块的具体使用方法进行描述。所有的例程均来自MSPM0 SDK的安装包&#xff0c;具体可到官网下载并安装: https://www.ti…

大创项目推荐 深度学习验证码识别 - 机器视觉 python opencv

文章目录 0 前言1 项目简介2 验证码识别步骤2.1 灰度处理&二值化2.2 去除边框2.3 图像降噪2.4 字符切割2.5 识别 3 基于tensorflow的验证码识别3.1 数据集3.2 基于tf的神经网络训练代码 4 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x…

009 Linux_文件系统 | 软硬链接

前言 本文将会向你介绍文件系统与软硬链接 文章重点 本文将会先向你介绍文件是如何在磁盘上进行管理的&#xff0c;关于文件的管理将会从管理属性和管理内容两方面来谈&#xff0c;最后会向你介绍软硬链接的概念 文件在磁盘中的管理 首先&#xff0c;假设一个磁盘200GB&#…

《WebKit 技术内幕》之五(2): HTML解释器和DOM 模型

2.HTML 解释器 2.1 解释过程 HTML 解释器的工作就是将网络或者本地磁盘获取的 HTML 网页和资源从字节流解释成 DOM 树结构。 这一过程中&#xff0c;WebKit 内部对网页内容在各个阶段的结构表示。 WebKit 中这一过程如下&#xff1a;首先是字节流&#xff0c;经过解码之…