【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | Android 端实现 MethodChannel 通信 )
文章目錄
- 前言
- 一、Android 端 MethodChannel 構(gòu)造函數(shù)
- 二、Android 端 setMethodCallHandler 方法
- 三、Android 端實(shí)現(xiàn) MethodChannel 通信步驟
- 四、相關(guān)資源
前言
本博客與 【Flutter】Flutter 混合開發(fā) ( Flutter 與 Native 通信 | 在 Flutter 端實(shí)現(xiàn) MethodChannel 通信 ) 博客相對(duì)應(yīng) , 該博客中開發(fā) Flutter 的 Dart 端 ;
本博客中開發(fā) Android 中的 Java 端 , 最終目標(biāo)是二者可以進(jìn)行信息交流 ;
一、Android 端 MethodChannel 構(gòu)造函數(shù)
Android 端 Java 中 , MethodChannel 構(gòu)造函數(shù)方法原型如下 :
public class MethodChannel {private static final String TAG = "MethodChannel#";private final BinaryMessenger messenger;private final String name;private final MethodCodec codec;/*** Creates a new channel associated with the specified {@link BinaryMessenger} and with the* specified name and the standard {@link MethodCodec}.** @param messenger a {@link BinaryMessenger}.* @param name a channel name String.*/public MethodChannel(BinaryMessenger messenger, String name) {this(messenger, name, StandardMethodCodec.INSTANCE);}/*** Creates a new channel associated with the specified {@link BinaryMessenger} and with the* specified name and {@link MethodCodec}.** @param messenger a {@link BinaryMessenger}.* @param name a channel name String.* @param codec a {@link MessageCodec}.*/public MethodChannel(BinaryMessenger messenger, String name, MethodCodec codec) {if (BuildConfig.DEBUG) {if (messenger == null) {Log.e(TAG, "Parameter messenger must not be null.");}if (name == null) {Log.e(TAG, "Parameter name must not be null.");}if (codec == null) {Log.e(TAG, "Parameter codec must not be null.");}}this.messenger = messenger;this.name = name;this.codec = codec;} }BasicMessageChannel 接收 333 個(gè)參數(shù) :
- BinaryMessenger messenger : 用于 發(fā)送 / 接收消息 ;
- String name : Channel 消息通道的名稱 , 該名稱必須與 Dart 中的消息通道名稱相同 ;
- MethodCodec codec : 方法編解碼器 ;
二、Android 端 setMethodCallHandler 方法
創(chuàng)建了 MethodChannel 實(shí)例對(duì)象后 , 如果要接收 Dart 端發(fā)送來的消息 , 需要設(shè)置 方法回調(diào)處理器 ;
調(diào)用 setMethodCallHandler 方法 , 可以為 MethodChannel 設(shè)置一個(gè) 方法回調(diào)處理器 ;
MethodChannel.setMethodCallHandler 函數(shù)原型如下 :
/*** Registers a method call handler on this channel.** <p>Overrides any existing handler registration for (the name of) this channel.** <p>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 <a* href="https://api.flutter.dev/flutter/services/MissingPluginException-class.html">MissingPluginException</a>* on the Dart side, unless an <a* href="https://api.flutter.dev/flutter/services/OptionalMethodChannel-class.html">OptionalMethodChannel</a>* is used.** @param handler a {@link MethodCallHandler}, or null to deregister.*/@UiThreadpublic void setMethodCallHandler(final @Nullable MethodCallHandler handler) {messenger.setMessageHandler(name, handler == null ? null : new IncomingMethodCallHandler(handler));}設(shè)置的 final @Nullable MethodCallHandler handler 參數(shù) , 就是 方法回調(diào)處理器 ;
在 MethodCallHandler 接口中 , 只有一個(gè) onMethodCall 方法 , 該方法是用于接收 Dart 傳遞來的消息的 ;
void onMethodCall(@NonNull MethodCall call, @NonNull Result result);onMethodCall 參數(shù)簡介 :
- MethodCall call : Dart 端傳遞來的消息 ;
- Result result : 向 Dart 端回傳的數(shù)據(jù) ;
MessageHandler 接口原型如下 :
/** A handler of incoming method calls. */public interface MethodCallHandler {/*** Handles the specified method call received from Flutter.** <p>Handler implementations must submit a result for all incoming calls, by making a single* call on the given {@link Result} callback. Failure to do so will result in lingering Flutter* result handlers. The result may be submitted asynchronously. Calls to unknown or* unimplemented methods should be handled using {@link Result#notImplemented()}.** <p>Any uncaught exception thrown by this method will be caught by the channel implementation* and logged, and an error result will be sent back to Flutter.** <p>The handler is called on the platform thread (Android main thread). For more details see* <a href="https://github.com/flutter/engine/wiki/Threading-in-the-Flutter-Engine">Threading in* the Flutter Engine</a>.** @param call A {@link MethodCall}.* @param result A {@link Result} used for submitting the result of the call.*/@UiThreadvoid onMethodCall(@NonNull MethodCall call, @NonNull Result result);}在 MethodCall 中 , 主要有兩個(gè)成員變量 :
- String method : 表示調(diào)用的方法名 ;
- Object arguments : 表示調(diào)用的參數(shù) ;
Result 接口中提供了 333 個(gè)方法 , 根據(jù)不同的結(jié)果 , 回調(diào)不同的接口方法 ;
- void success(@Nullable Object result) : 表示調(diào)用成功 ;
- error(String errorCode, @Nullable String errorMessage, @Nullable Object errorDetails) : 表示出現(xiàn)錯(cuò)誤 ;
- void notImplemented() : 表示要調(diào)用的函數(shù)在 Dart 端沒有實(shí)現(xiàn) ;
三、Android 端實(shí)現(xiàn) MethodChannel 通信步驟
Android 端實(shí)現(xiàn) MethodChannel 通信步驟 :
首先 , 初始化 MethodChannel 實(shí)例對(duì)象 ;
MethodChannel mMethodChannel = new MethodChannel(mFlutterFragment.getFlutterEngine().getDartExecutor(), "MethodChannel");然后 , 為 MethodChannel 實(shí)例對(duì)象 設(shè)置 MethodChannel.MethodCallHandler , 用于接收 Flutter 端調(diào)用 Android 端方法 ;
mMethodChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {@Overridepublic void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {show_message.setText("Dart 端通過 MethodChannel 調(diào)用 Android 端的 " + call.method + " 方法 , 參數(shù)是 " + call.arguments);} });最后 , 調(diào)用 mMethodChannel 的 invokeMethod 方法 , 調(diào)用 Flutter 中的方法 ;
findViewById(R.id.channel3).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {mMethodChannel.invokeMethod("method", "arguments");} });四、相關(guān)資源
參考資料 :
- Flutter 官網(wǎng) : https://flutter.dev/
- Flutter 插件下載地址 : https://pub.dev/packages
- Flutter 開發(fā)文檔 : https://flutter.cn/docs ( 強(qiáng)烈推薦 )
- 官方 GitHub 地址 : https://github.com/flutter
- Flutter 中文社區(qū) : https://flutter.cn/
- Flutter 實(shí)用教程 : https://flutter.cn/docs/cookbook
- Flutter CodeLab : https://codelabs.flutter-io.cn/
- Dart 中文文檔 : https://dart.cn/
- Dart 開發(fā)者官網(wǎng) : https://api.dart.dev/
- Flutter 中文網(wǎng) : https://flutterchina.club/ , http://flutter.axuer.com/docs/
- Flutter 相關(guān)問題 : https://flutterchina.club/faq/ ( 入門階段推薦看一遍 )
- GitHub 上的 Flutter 開源示例 : https://download.csdn.net/download/han1202012/15989510
- Flutter 實(shí)戰(zhàn)電子書 : https://book.flutterchina.club/chapter1/
- Dart 語言練習(xí)網(wǎng)站 : https://dartpad.dartlang.org/
重要的專題 :
- Flutter 動(dòng)畫參考文檔 : https://flutterchina.club/animations/
博客源碼下載 :
-
GitHub 地址 : ( 隨博客進(jìn)度一直更新 , 有可能沒有本博客的源碼 )
- Flutter Module 工程 : https://github.com/han1202012/flutter_module
- Android 應(yīng)用 : https://github.com/han1202012/flutter_native
- 注意 : 上面兩個(gè)工程要放在同一個(gè)目錄中 , 否則編譯不通過 ;
-
博客源碼快照 : https://download.csdn.net/download/han1202012/21670919 ( 本篇博客的源碼快照 , 可以找到本博客的源碼 )
總結(jié)
以上是生活随笔為你收集整理的【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | Android 端实现 MethodChannel 通信 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Flutter】Flutter 混合开
- 下一篇: 【Flutter】Flutter 混合开