Class MethodChannel
Incoming method calls are decoded from binary on receipt, and Java results are encoded into
binary before being transmitted back to Flutter. The MethodCodec
used must be compatible
with the one used by the Flutter application. This can be achieved by creating a MethodChannel
counterpart of this channel on the Dart side. The Java type of method call arguments and results
is Object
, but only values supported by the specified MethodCodec
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
Modifier and TypeClassDescriptionstatic interface
A handler of incoming method calls.static interface
Method call result callback. -
Constructor Summary
ConstructorDescriptionMethodChannel
(BinaryMessenger messenger, String name) Creates a new channel associated with the specifiedBinaryMessenger
and with the specified name and the standardMethodCodec
.MethodChannel
(BinaryMessenger messenger, String name, MethodCodec codec) Creates a new channel associated with the specifiedBinaryMessenger
and with the specified name andMethodCodec
.MethodChannel
(BinaryMessenger messenger, String name, MethodCodec codec, BinaryMessenger.TaskQueue taskQueue) Creates a new channel associated with the specifiedBinaryMessenger
and with the specified name andMethodCodec
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
invokeMethod
(String method, Object arguments) Invokes a method on this channel, expecting no result.void
invokeMethod
(String method, Object arguments, MethodChannel.Result callback) Invokes a method on this channel, optionally expecting a result.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.void
Registers a method call handler on this channel.void
setWarnsOnChannelOverflow
(boolean warns) Toggles whether the channel should show warning messages when discarding messages due to overflow.
-
Constructor Details
-
MethodChannel
Creates a new channel associated with the specifiedBinaryMessenger
and with the specified name and the standardMethodCodec
.- Parameters:
messenger
- aBinaryMessenger
.name
- a channel name String.
-
MethodChannel
public MethodChannel(@NonNull BinaryMessenger messenger, @NonNull String name, @NonNull MethodCodec codec) Creates a new channel associated with the specifiedBinaryMessenger
and with the specified name andMethodCodec
.- Parameters:
messenger
- aBinaryMessenger
.name
- a channel name String.codec
- aMessageCodec
.
-
MethodChannel
public MethodChannel(@NonNull BinaryMessenger messenger, @NonNull String name, @NonNull MethodCodec codec, @Nullable BinaryMessenger.TaskQueue taskQueue) Creates a new channel associated with the specifiedBinaryMessenger
and with the specified name andMethodCodec
.- Parameters:
messenger
- aBinaryMessenger
.name
- a channel name String.codec
- aMessageCodec
.taskQueue
- aBinaryMessenger.TaskQueue
that specifies what thread will execute the handler. Specifying null means execute on the platform thread. See alsoBinaryMessenger.makeBackgroundTaskQueue()
.
-
-
Method Details
-
invokeMethod
Invokes a method on this channel, expecting no result.- Parameters:
method
- the name String of the method.arguments
- the arguments for the invocation, possibly null.
-
invokeMethod
@UiThread public void invokeMethod(@NonNull String method, @Nullable Object arguments, @Nullable MethodChannel.Result callback) Invokes a method on this channel, optionally expecting a result.Any uncaught exception thrown by the result callback will be caught and logged.
- Parameters:
method
- the name String of the method.arguments
- the arguments for the invocation, possibly null.callback
- aMethodChannel.Result
callback for the invocation result, or null.
-
setMethodCallHandler
Registers a method call handler on this channel.Overrides any existing handler registration for (the name of) this channel.
If no handler has been registered, any incoming method call on this channel will be handled silently by sending a null reply. This results in a MissingPluginException on the Dart side, unless an OptionalMethodChannel is used.
- Parameters:
handler
- aMethodChannel.MethodCallHandler
, 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.
-