Flutter 与Android 的通讯 -BasicMessageChannel方式

Flutter 端

static const BasicMessageChannel<String> messageChannel = BasicMessageChannel<String>(“flutter-drumkaka”, StringCodec());
var receivedMessage = “”
//messageChannel.setMessageHandler(_handlePlatformIncrement);
Future<String> _handleNativeMessage(String message) async {
print(“message=$message”);
setState(() {

receivedMessage = message;
});
return “a return message”;
}
_handNativeMessage(“this is from Flutter”);

 

Android 端

String CHANNEL = "flutter-drumkaka";
BasicMessageChannel<String> messageChannel = new BasicMessageChannel<>(getFlutterEngine().getDartExecutor().getBinaryMessenger(), CHANNEL, StringCodec.INSTANCE);
messageChannel.
        setMessageHandler(new BasicMessageChannel.MessageHandler<String>() {

            @Override
            public void onMessage(@Nullable String message, @NonNull BasicMessageChannel.Reply<String> reply) {
                showMessage(message);                
            }
        });

messageChannel.send("this is from Android Native");

发表评论