Class MethodChannel
- java.lang.Object
-
- io.flutter.plugin.common.MethodChannel
-
public class MethodChannel extends Object
A named channel for communicating with the Flutter application using asynchronous method calls.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 isObject
, but only values supported by the specifiedMethodCodec
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
MethodChannel.MethodCallHandler
A handler of incoming method calls.static interface
MethodChannel.Result
Method call result callback.
-
Constructor Summary
Constructors Constructor Description MethodChannel(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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
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
setMethodCallHandler(MethodChannel.MethodCallHandler handler)
Registers a method call handler on this channel.
-
-
-
Constructor Detail
-
MethodChannel
public MethodChannel(@NonNull BinaryMessenger messenger, @NonNull String name)
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 Detail
-
invokeMethod
@UiThread public void invokeMethod(@NonNull String method, @Nullable Object arguments)
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
@UiThread public void setMethodCallHandler(@Nullable MethodChannel.MethodCallHandler handler)
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.
-
-