日韩性视频-久久久蜜桃-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)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: av在线片 | 男女日批免费视频 | 亚洲激情电影在线 | 亚洲又粗又长 | 91在线色| 黄色理论视频 | 国产精品福利网站 | 日韩三区在线 | 欧美七区| 爱啪啪影视 | 国产爱搞 | 久久日本精品字幕区二区 | 美女国产网站 | av福利在线观看 | 欧美日韩精品在线视频 | 亚洲成年人免费观看 | 久久久久影视 | 这里只有精品国产 | 爱射综合 | 一区二区日本视频 | 91av视频在线免费观看 | 日韩av一区二区在线播放 | 97在线观看视频免费 | www.av777| 日本黄色网页 | 色先锋在线 | 国产伦精品一区二区三区高清版禁 | 校园春色在线观看 | 黄色在线观看国产 | 加勒比视频在线观看 | 精品国产一区二区三区四区 | 亚洲一区二区三区综合 | 成人午夜看片 | 欧美专区日韩专区 | 成人在线视频一区二区三区 | 在线观看三区 | 欧洲黄色网 | 欧洲自拍偷拍 | 91在线精品一区二区 | 欧美丰满熟妇xxxx | 香蕉日日 | 欧美视频一 | 欧美激情视频在线观看 | 精品久久久久久久无码 | 欧美亚洲高清 | 黑丝啪啪 | 久99热| 欧美第一页 | 毛片毛片女人毛片毛片 | 国产日韩专区 | 日韩大尺度在线观看 | 国产探花在线精品一区二区 | 欧美视频一区二区三区 | 欧美国产日韩一区二区三区 | 日本sm调教—视频|vk | 四虎成人精品永久免费av九九 | 少女忠诚电影高清免费 | 亚洲清纯唯美 | 精品一区二区无码 | 国产91av视频 | 成人激情四射网 | 吊视频一区二区三区 | 狠狠操天天操夜夜操 | 久久综合九色综合欧美狠狠 | 中文字幕在线观看精品 | 深爱激情av| 嫩草视频在线观看 | 久久yy | 黄色片久久 | 国产日韩在线观看视频 | 久久777 | 亚州精品毛片 | 公侵犯人妻一区二区三区 | 91网站免费观看 | 国内国产精品天干天干 | 国产成人精品一区二三区四区五区 | 在线视频中文字幕 | 亚洲一区二区电影网 | 免费一级黄色 | 国产一区二区在线观看视频 | 色哟哟网站在线观看 | 亚洲黄色一区 | 色偷偷免费费视频在线 | 国产av无码专区亚洲av | 在线亚洲天堂 | 欧美色99| 国产婷婷一区二区三区久久 | 人人搞人人 | 中文在线免费观看 | 国产黄色自拍 | 欧美精品欧美精品系列 | 自拍日韩亚洲一区在线 | 欧美精品一区二区三区在线播放 | 一区二区日韩国产 | 五月婷婷七月丁香 | 九月婷婷综合 | 特黄一级片 | 日本最新中文字幕 | youjizz麻豆 |