public final class BasicMessageChannel<T> extends Object
Messages are encoded into binary before being sent, and binary messages received are decoded
into Java objects. The MessageCodec
used must be compatible with the
one used by the Flutter application. This can be achieved by creating a
BasicMessageChannel
counterpart of this channel on the Dart side. The static Java type of messages sent and received
is Object
, but only values supported by the specified MessageCodec
can be used.
The logical identity of the channel is given by its name. Identically named channels will interfere with each other's communication.
Modifier and Type | Class and Description |
---|---|
static interface |
BasicMessageChannel.MessageHandler<T>
A handler of incoming messages.
|
static interface |
BasicMessageChannel.Reply<T>
Message reply callback.
|
Constructor and Description |
---|
BasicMessageChannel(BinaryMessenger messenger,
String name,
MessageCodec<T> codec)
Creates a new channel associated with the specified
BinaryMessenger
and with the specified name and MessageCodec . |
Modifier and Type | Method and Description |
---|---|
void |
send(T message)
Sends the specified message to the Flutter application on this channel.
|
void |
send(T message,
BasicMessageChannel.Reply<T> callback)
Sends the specified message to the Flutter application, optionally expecting a reply.
|
void |
setMessageHandler(BasicMessageChannel.MessageHandler<T> handler)
Registers a message handler on this channel for receiving messages sent from the Flutter
application.
|
public BasicMessageChannel(BinaryMessenger messenger, String name, MessageCodec<T> codec)
BinaryMessenger
and with the specified name and MessageCodec
.messenger
- a BinaryMessenger
.name
- a channel name String.codec
- a MessageCodec
.public void send(T message)
message
- the message, possibly null.public void send(T message, BasicMessageChannel.Reply<T> callback)
Any uncaught exception thrown by the reply callback will be caught and logged.
message
- the message, possibly null.callback
- a BasicMessageChannel.Reply
callback, possibly null.public void setMessageHandler(BasicMessageChannel.MessageHandler<T> handler)
Overrides any existing handler registration for (the name of) this channel.
If no handler has been registered, any incoming message on this channel will be handled silently by sending a null reply.
handler
- a BasicMessageChannel.MessageHandler
, or null to deregister.