文章目录
- (一)通过MessageChannel实现Flutter与原生平台之间的双向通信
- (二)通过MethodChannel实现Flutter与原生平台之间的双向通信
- (三)通过EventChannel实现Flutter与原生平台之间的双向通信
首先,现在Android中进行注册与初始化:
java
private void registerChannelFunction() {
FlutterEngine flutterEngine = getFlutterEngine();
if (flutterEngine == null) {
Log.e("ERROR", "注册消息通道失败 FlutterEngine = null");
return;
}
DartExecutor dartExecutor = flutterEngine.getDartExecutor();
binaryMessenger = dartExecutor.getBinaryMessenger();
messageChannelSendFunction();
methodChannelSendFunction();
eventChannelSendFunction();
}
(一)通过MessageChannel实现Flutter与原生平台之间的双向通信
Flutter端实现MessageChannel通信步骤:
先定义messageChannel
dart
static const basicMethodChannel =
BasicMessageChannel("flutter_basic_method_channel", StandardMessageCodec());
发送message:
dart
Future<void> sendMessage(Map json) async {
await basicMethodChannel.send(json);
}
接收message:
dart
void receiveMessage() {
basicMethodChannel.setMessageHandler((result) async {
Map? map = result as Map?;
int code = map?["code"];
String message=map?["message"];
setState(() {
receive = "messageChannel receiveMessage 中的回调 code $code message $message";
print(receive);
print("receiveMessage: code:$code message:$message");
});
return 'Flutter 已收到消息';
});
}
Android端实现MessageChannel通信步骤:
首先,先初始化 BasicMessageChannel 实例对象:
java
basicMessageChannel = new BasicMessageChannel<>(binaryMessenger,
"flutter_basic_method_channel", StandardMessageCodec.INSTANCE);
然后为Message对象设置MessageHandler对象:
java
basicMessageChannel.setMessageHandler(new BasicMessageChannel.MessageHandler() {
@Override
public void onMessage(@Nullable Object message, @NonNull BasicMessageChannel.Reply reply) {
System.out.println("flutter 传来的数据为: "+message.toString());
basicSendMessage();
}
});
(二)通过MethodChannel实现Flutter与原生平台之间的双向通信
Flutter端实现MessageChannel通信步骤:
定义methodChannel:
java
static const methodChannel = MethodChannel("flutter_method_channel");
然后通过methodChannel的invokeMethod进行消息的发送:
java
static Future<dynamic> invokeNative(String method,
{required Map<String, String> arguments}) async {
if (arguments == null) {
return await methodChannel.invokeMethod(method);
} else {
return await methodChannel.invokeMethod(method, arguments);
}
}
最后调用会返回一个结果,通过这个result拿到返回的数据:
java
Map result = await invokeNative("test", arguments: {});
int code = result["code"];
String message = result["message"];
setState(() {
receive = "invokeNative 中的回调 code $code message $message";
print(receive);
});
Android端实现MessageChannel通信步骤:
先初始化MethodChannel的实例:
java
mMethodChannel = new MethodChannel(binaryMessenger, channelName);
然后设置methodHandler,它会重写MethodCallHandler的onMethodCall方法:
java
private MethodChannel.MethodCallHandler getMethodCallHandler() {
return new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
String method = call.method;
Object Arguments = call.arguments;
Map<String, Object> arguments = null;
if (null != Arguments) {
arguments = (Map<String, Object>) Arguments;
}
if ("test".equals(method)) {
Toast.makeText(this, "flutter 调用到了 android test 数据为 name :"+arguments.get("name") +" age : "+arguments.get("age"), Toast.LENGTH_SHORT).show();
Map<String, Object> objectMap = new HashMap<>();
objectMap.put("message", "result.success 返回给flutter的basic数据");
objectMap.put("code", 300);
result.success(objectMap);
} else {
result.notImplemented();
}
}
};
}
(三)通过EventChannel实现Flutter与原生平台之间的双向通信
Flutter端实现EventChannel通信步骤:
首先 初始化EventChannel实例:
java
static const eventChannel = EventChannel("flutter_event_channel");
然后设置Stream:
java
eventChannel.receiveBroadcastStream().listen((event) {
// int code = event["code"];
// String message = event["message"];
setState(() {
// receive += "code $code message $message ";
// print("eventMessageListener: "+receive);
print(event);
});
}, onError: (event) {});
Android端实现EventChannel通信步骤:
初始化EventChannel:
java
String channelName = "flutter_event_channel";
EventChannel eventChannel = new EventChannel(binaryMessenger, channelName);
然后调用setStreamHandler为EventChannel设置一个处理器:
java
eventChannel.setStreamHandler(new EventChannel.StreamHandler() {
@Override
public void onListen(Object arguments, EventChannel.EventSink events) {
eventSink = events;
startTimer();
}
@Override
public void onCancel(Object arguments) {
}
});
EventSink 接口中 , 有 3 个方法 :
- success : 表示接收数据成功 ;
- error : 表示接收数据出现错误 ;
- endOfStream : 表示接收数据结束 ;
这样就完成了Flutter与Android原生之间的通信,IOS的不太熟,有需要可以搜一下,都大差不差,如果觉得博主写的对你有帮助,麻烦给个一键三连