Package io.flutter.plugin.common

# Class BasicMessageChannel<T>

java.lang.Object
io.flutter.plugin.common.BasicMessageChannel<T>

public final class BasicMessageChannel<T> extends Object
A named channel for communicating with the Flutter application using basic, asynchronous message passing.

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.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static interface`
  `BasicMessageChannel.MessageHandler<T>`
  A handler of incoming messages.
  `static interface`
  `BasicMessageChannel.Reply<T>`
  Message reply callback.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final String`
  `CHANNEL_BUFFERS_CHANNEL`

- ## Constructor Summary

  Constructors
  Constructor
  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`.
  `BasicMessageChannel(BinaryMessenger messenger, String name, MessageCodec<T> codec, BinaryMessenger.TaskQueue taskQueue)`
  Creates a new channel associated with the specified `BinaryMessenger` and with the specified name and `MessageCodec`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `resizeChannelBuffer(int newSize)`
  Adjusts the number of messages that will get buffered when sending messages to channels that aren't fully set up yet.
  `static void`
  `resizeChannelBuffer(BinaryMessenger messenger, String channel, int newSize)`
  Adjusts the number of messages that will get buffered when sending messages to channels that aren't fully set up yet.
  `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.
  `void`
  `setWarnsOnChannelOverflow(boolean warns)`
  Toggles whether the channel should show warning messages when discarding messages due to overflow.
  `static void`
  `setWarnsOnChannelOverflow(BinaryMessenger messenger, String channel, boolean warns)`
  Toggles whether the channel should show warning messages when discarding messages due to overflow.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### CHANNEL_BUFFERS_CHANNEL

    public static final String CHANNEL_BUFFERS_CHANNEL
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### BasicMessageChannel

    public BasicMessageChannel(@NonNull BinaryMessenger messenger, @NonNull String name, @NonNull MessageCodec<T> codec)
    Creates a new channel associated with the specified `BinaryMessenger` and with the specified name and `MessageCodec`.
    Parameters:
      `messenger` - a `BinaryMessenger`.
      `name` - a channel name String.
      `codec` - a `MessageCodec`.

  - ### BasicMessageChannel

    public BasicMessageChannel(@NonNull BinaryMessenger messenger, @NonNull String name, @NonNull MessageCodec<T> codec, BinaryMessenger.TaskQueue taskQueue)
    Creates a new channel associated with the specified `BinaryMessenger` and with the specified name and `MessageCodec`.
    Parameters:
      `messenger` - a `BinaryMessenger`.
      `name` - a channel name String.
      `codec` - a `MessageCodec`.
      `taskQueue` - a `BinaryMessenger.TaskQueue` that specifies what thread will execute the handler. Specifying null means execute on the platform thread. See also `BinaryMessenger.makeBackgroundTaskQueue()`.

- ## Method Details

  - ### send

    public void send(@Nullable T message)
    Sends the specified message to the Flutter application on this channel.
    Parameters:
      `message` - the message, possibly null.

  - ### send

    @UiThread public void send(@Nullable T message, @Nullable BasicMessageChannel.Reply<T> callback)
    Sends the specified message to the Flutter application, optionally expecting a reply.

    Any uncaught exception thrown by the reply callback will be caught and logged.

    Parameters:
      `message` - the message, possibly null.
      `callback` - a `BasicMessageChannel.Reply` callback, possibly null.

  - ### setMessageHandler

    @UiThread public void setMessageHandler(@Nullable BasicMessageChannel.MessageHandler<T> handler)
    Registers a message handler on this channel for receiving messages sent from the Flutter application.

    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.

    Parameters:
      `handler` - a `BasicMessageChannel.MessageHandler`, or null to deregister.

  - ### resizeChannelBuffer

    public void resizeChannelBuffer(int newSize)
    Adjusts the number of messages that will get buffered when sending messages to channels that aren't fully set up yet. For example, the engine isn't running yet or the channel's message handler isn't set up on the Dart side yet.

  - ### setWarnsOnChannelOverflow

    public void setWarnsOnChannelOverflow(boolean warns)
    Toggles whether the channel should show warning messages when discarding messages due to overflow. When 'warns' is false the channel is expected to overflow and warning messages will not be shown.

  - ### resizeChannelBuffer

    public static void resizeChannelBuffer(@NonNull BinaryMessenger messenger, @NonNull String channel, int newSize)
    Adjusts the number of messages that will get buffered when sending messages to channels that aren't fully set up yet. For example, the engine isn't running yet or the channel's message handler isn't set up on the Dart side yet.

  - ### setWarnsOnChannelOverflow

    public static void setWarnsOnChannelOverflow(@NonNull BinaryMessenger messenger, @NonNull String channel, boolean warns)
    Toggles whether the channel should show warning messages when discarding messages due to overflow. When 'warns' is false the channel is expected to overflow and warning messages will not be shown.