合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
[TOC] ## BasicMessageChannel 用于传递字符串和半结构化的信息 ``` BasicMessageChannel(String name, MessageCodec<T> codec, {BinaryMessenger binaryMessenger}) BinaryCodec 发送二进制消息时 JSONMessageCodec 发送Json格式消息时 StandardMessageCodec 发送基本型数据时 StringCodec 发送String类型消息时 ``` demo: flutter 端 ``` class _MyHomePageState extends State<MyHomePage> { static const _channel = BasicMessageChannel('com.example.messagechannel/interop', StringCodec()); String _platformMessage; void _sendMessage() async { final String reply = await _channel.send('Hello World form Dart'); print(reply); } @override initState() { super.initState(); // Receive messages from platform _channel.setMessageHandler((String message) async { print('Received message = $message'); setState(() => _platformMessage = message); return 'Reply from Dart'; }); // Send message to platform _sendMessage(); } ``` andriod ``` class MainActivity: FlutterActivity() { override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { GeneratedPluginRegistrant.registerWith(flutterEngine) val channel = BasicMessageChannel( flutterEngine.dartExecutor.binaryMessenger, "com.example.messagechannel/interop", StringCodec.INSTANCE) // Receive messages from Dart channel.setMessageHandler { message, reply -> Log.d("Android", "Received message = $message") reply.reply("Reply from Android") } // Send message to Dart Handler().postDelayed({ channel.send("Hello World from Android") { reply -> Log.d("Android", "$reply") } }, 500) } } ``` ## EventChannel 用于数据流(event streams)的通信 fultter ``` var _eventChannel = EventChannel('com.flutter.guide.EventChannel'); var _data; @override void initState() { super.initState(); _eventChannel.receiveBroadcastStream().listen(_onData); } _onData(event){ setState(() { _data = event; }); } ``` andriod ``` class EventChannelDemo(var activity: Activity, messenger: BinaryMessenger):EventChannel.StreamHandler { private var channel: EventChannel private var index = 0 private var events: EventChannel.EventSink? = null init { channel = EventChannel(messenger, "com.flutter.guide.EventChannel") channel.setStreamHandler(this) startTimer() } fun startTimer() { var timer = Timer().schedule(timerTask { index++ var map = mapOf("name" to "laomeng${index}", "age" to "${index}" ) activity.runOnUiThread { events?.success(map) } }, 0, 1000) } override fun onListen(arguments: Any?, events: EventChannel.EventSink?) { this.events = events } override fun onCancel(arguments: Any?) { this.events = null } } ``` ## MethodChannel 用手传递方法调用(method invocation) flutter ``` String? _message=''; static const _channel = MethodChannel("mChanel",); // 调用 final String result = (await _channel.invokeMethod("nativeMethod", ``` 安卓端 ``` override fun configureFlutterEngine(flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "mChanel") .setMethodCallHandler { call: MethodCall, result: MethodChannel.Result -> if (call.method == "nativeMethod") { val parameter = call.argument<String>("param") // 在这里处理参数并返回结果 result.success("Native method called with parameter: $parameter") } else { result.notImplemented() } } } ```