日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | Android 端实现 MethodChannel 通信 )

發(fā)布時間:2025/6/17 Android 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | Android 端实现 MethodChannel 通信 ) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 前言
  • 一、Android 端 MethodChannel 構(gòu)造函數(shù)
  • 二、Android 端 setMethodCallHandler 方法
  • 三、Android 端實現(xiàn) MethodChannel 通信步驟
  • 四、相關(guān)資源

前言

本博客與 【Flutter】Flutter 混合開發(fā) ( Flutter 與 Native 通信 | 在 Flutter 端實現(xiàn) MethodChannel 通信 ) 博客相對應 , 該博客中開發(fā) Flutter 的 Dart 端 ;

本博客中開發(fā) Android 中的 Java 端 , 最終目標是二者可以進行信息交流 ;





一、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 個參數(shù) :

  • BinaryMessenger messenger : 用于 發(fā)送 / 接收消息 ;
  • String name : Channel 消息通道的名稱 , 該名稱必須與 Dart 中的消息通道名稱相同 ;
  • MethodCodec codec : 方法編解碼器 ;




二、Android 端 setMethodCallHandler 方法



創(chuàng)建了 MethodChannel 實例對象后 , 如果要接收 Dart 端發(fā)送來的消息 , 需要設置 方法回調(diào)處理器 ;

調(diào)用 setMethodCallHandler 方法 , 可以為 MethodChannel 設置一個 方法回調(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));}

設置的 final @Nullable MethodCallHandler handler 參數(shù) , 就是 方法回調(diào)處理器 ;

在 MethodCallHandler 接口中 , 只有一個 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 中 , 主要有兩個成員變量 :

  • String method : 表示調(diào)用的方法名 ;
  • Object arguments : 表示調(diào)用的參數(shù) ;
/** Command object representing a method call on a {@link MethodChannel}. */ public final class MethodCall {/** The name of the called method. */public final String method;/*** Arguments for the call.** <p>Consider using {@link #arguments()} for cases where a particular run-time type is expected.* Consider using {@link #argument(String)} when that run-time type is {@link Map} or {@link* JSONObject}.*/public final Object arguments; }

Result 接口中提供了 333 個方法 , 根據(jù)不同的結(jié)果 , 回調(diào)不同的接口方法 ;

  • void success(@Nullable Object result) : 表示調(diào)用成功 ;
  • error(String errorCode, @Nullable String errorMessage, @Nullable Object errorDetails) : 表示出現(xiàn)錯誤 ;
  • void notImplemented() : 表示要調(diào)用的函數(shù)在 Dart 端沒有實現(xiàn) ;
/*** Method call result callback. Supports dual use: Implementations of methods to be invoked by* Flutter act as clients of this interface for sending results back to Flutter. Invokers of* Flutter methods provide implementations of this interface for handling results received from* Flutter.** <p>All methods of this class must be 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>.*/public interface Result {/*** Handles a successful result.** @param result The result, possibly null. The result must be an Object type supported by the* codec. For instance, if you are using {@link StandardMessageCodec} (default), please see* its documentation on what types are supported.*/@UiThreadvoid success(@Nullable Object result);/*** Handles an error result.** @param errorCode An error code String.* @param errorMessage A human-readable error message String, possibly null.* @param errorDetails Error details, possibly null. The details must be an Object type* supported by the codec. For instance, if you are using {@link StandardMessageCodec}* (default), please see its documentation on what types are supported.*/@UiThreadvoid error(String errorCode, @Nullable String errorMessage, @Nullable Object errorDetails);/** Handles a call to an unimplemented method. */@UiThreadvoid notImplemented();}



三、Android 端實現(xiàn) MethodChannel 通信步驟



Android 端實現(xiàn) MethodChannel 通信步驟 :

首先 , 初始化 MethodChannel 實例對象 ;

MethodChannel mMethodChannel = new MethodChannel(mFlutterFragment.getFlutterEngine().getDartExecutor(), "MethodChannel");

然后 , 為 MethodChannel 實例對象 設置 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 ( 強烈推薦 )
  • 官方 GitHub 地址 : https://github.com/flutter
  • Flutter 中文社區(qū) : https://flutter.cn/
  • Flutter 實用教程 : 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 實戰(zhàn)電子書 : https://book.flutterchina.club/chapter1/
  • Dart 語言練習網(wǎng)站 : https://dartpad.dartlang.org/

重要的專題 :

  • Flutter 動畫參考文檔 : https://flutterchina.club/animations/

博客源碼下載 :

  • GitHub 地址 : ( 隨博客進度一直更新 , 有可能沒有本博客的源碼 )

    • Flutter Module 工程 : https://github.com/han1202012/flutter_module
    • Android 應用 : https://github.com/han1202012/flutter_native
    • 注意 : 上面兩個工程要放在同一個目錄中 , 否則編譯不通過 ;
  • 博客源碼快照 : https://download.csdn.net/download/han1202012/21670919 ( 本篇博客的源碼快照 , 可以找到本博客的源碼 )

總結(jié)

以上是生活随笔為你收集整理的【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | Android 端实现 MethodChannel 通信 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。