Flutter學習筆記(29)--Flutter如何與native進行通信

来源:https://www.cnblogs.com/upwgh/archive/2019/10/12/11662640.html
-Advertisement-
Play Games

如需轉載,請註明出處:Flutter學習筆記(29)--Flutter如何與native進行通信 前言:在我們開發Flutter項目的時候,難免會遇到需要調用native api或者是其他的情況,這時候就需要處理Flutter與native的通信問題,一般常用的Flutter與native的通信方式 ...


如需轉載,請註明出處:Flutter學習筆記(29)--Flutter如何與native進行通信

前言:在我們開發Flutter項目的時候,難免會遇到需要調用native api或者是其他的情況,這時候就需要處理Flutter與native的通信問題,一般常用的Flutter與native的通信方式有3中。

1.MethodChannel:Flutter端向native端發送通知,通常用來調用native的某一個方法。

2.EventChannel:用於數據流的通信,有監聽功能,比如電量變化後直接推送給Flutter端。

3.BasicMessageChannel:用於傳遞字元串或半結構體的數據。

接下來具體看一下每種通信方式的使用方法!

  • MethodChannel

先來整體說一下邏輯思想吧,這樣能更容易理解一些,如果想要實現Flutter與native通信,首先要建立一個通信的通道,通過一個通道標識來進行匹配,匹配上了之後Flutter端通過invokeMethod調用方法來發起一個請求,在native端通過onMethodCall進行匹配請求的key,匹配上了就處理對應case內的邏輯!!!整體來看,我感覺有點EventBus的意思呢,就像是一條事件匯流排。。。

第一步:實現通信插件Plugin-native端

由於一個項目中可能會需要很多Flutter與native的通信,所以我這裡是將測試的插件封裝到一個類裡面了,然後在MainActivity裡面的onCreate進行註冊

package com.example.flutter_demo;

import android.content.Context;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;

public class TestPlugin implements MethodChannel.MethodCallHandler {
    public static String CHANNELNAME = "channel_name";//每一個通信通道的唯一標識,在整個項目內唯一!!!
    private static MethodChannel methodChannel;
    private Context context;

    public TestPlugin(Context context) {
        this.context = context;
    }

    public static void registerWith(PluginRegistry.Registrar registrar){
        methodChannel = new MethodChannel(registrar.messenger(),CHANNELNAME);
        TestPlugin instance = new TestPlugin(registrar.activity());
        methodChannel.setMethodCallHandler(instance);
    }

    @Override
    public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
        if (methodCall.method.equals("method_key")){
            result.success("what is up man???");
        }
    }
}

註:CHANNELNAME-->上面說過了,由於項目內會有很多的通信,所以我們定義的Channel必須是唯一的!!!!

TestPlugin實現MethodChannel.MethodCallHandler,定義一個對外暴露的註冊方法registerWith,因為我們需要在MainActivity進行註冊,在registerWith方法內初始化MethodChannel

接下來我們看一下onMethodCall方法,這個方法在Flutter發起請求時被調用,方法內有兩個參數,一個methodCall和一個result,我們分別來說一下這兩個參數:

methodCall:其中當前請求的相關信息,比如匹配請求的key

result:用於給Flutter返回數據,有3個方法,result.success(成功調用)、result.erro(失敗調用)、result.notImplemented(方法沒有實現調用)

第二步:註冊通信插件Plugin-native端

package com.example.flutter_demo;

import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
    TestPlugin.registerWith(this.registrarFor(TestPlugin.CHANNELNAME));
  }
}

註冊這塊我感覺作用是起到了一個橋梁的作用,通過註冊將插件和Flutter內定義的CHANNEL關聯了起來。

第三步:Flutter內發起通信請求-flutter端

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new MyAppState();
  }

}

class MyAppState extends State<MyApp> {
  var _textContent = 'welcome to flutter word';

  Future<Null> _changeTextContent() async{
    //channel_name每一個通信通道的唯一標識,在整個項目內唯一!!!
    const platfom = const MethodChannel('channel_name');
    try {
      //method_key是插件TestPlugin中onMethodCall回調匹配的key
      String resultValue = await platfom.invokeMethod('method_key');
      setState(() {
        _textContent = resultValue;
      });
    }on PlatformException catch (e){
      print(e.toString());
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new MaterialApp(
      theme: new ThemeData(
        primaryColor: Colors.white,
      ),
      debugShowCheckedModeBanner: false,
      title: 'demo',
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text('Demo'),
          leading: Icon(Icons.menu,size: 30,),
          actions: <Widget>[
            Icon(Icons.search,size: 30,)
          ],
        ),
        body: new Center(
          child: new Text(_textContent),
        ),
        floatingActionButton: new FloatingActionButton(onPressed: _changeTextContent,child: new Icon(Icons.adjust),),
      ),
    );
  }
}

這裡的功能就是頁面中央有一個text,通過點擊一個按鈕,發起通信請求,通信成功在就收到native返回的數據後將text的文案修改。

我們看一下最終的效果:

                

MethodChannel通信是雙向的,也就是說,Flutter端可以向native發起通信,native也可以向Flutter端發起通信,本質上就是反過來調用一下,原理上是同一個意思,具體的代碼就不在這裡寫了,需要的話可以自行百度一下!

  • EventChannel

EventChannel的使用我們也以官方獲取電池電量的demo為例,手機的電池狀態是不停變化的。我們要把這樣的電池狀態變化由Native及時通過EventChannel來告訴Flutter。這種情況用之前講的MethodChannel辦法是不行的,這意味著Flutter需要用輪詢的方式不停調用getBatteryLevel來獲取當前電量,顯然是不正確的做法。而用EventChannel的方式,則是將當前電池狀態"推送"給Flutter。

第一步:MainActivity內註冊EventChannel,並提供獲取電量的方法-native端

public class EventChannelPlugin implements EventChannel.StreamHandler {

    private Handler handler;
    private static final String CHANNEL = "com.example.flutter_battery/stream";
    private int count = 0;

    public static void registerWith(PluginRegistry.Registrar registrar) {
        // 新建 EventChannel, CHANNEL常量的作用和 MethodChannel 一樣的
        final EventChannel channel = new EventChannel(registrar.messenger(), CHANNEL);
        // 設置流的處理器(StreamHandler)
        channel.setStreamHandler(new EventChannelPlugin());
    }

    @Override
    public void onListen(Object o, EventChannel.EventSink eventSink) {
        // 每隔一秒數字+1
        handler = new Handler(message -> {
            // 然後把數字發送給 Flutter
            eventSink.success(++count);
            handler.sendEmptyMessageDelayed(0, 1000);
            return false;
        });
        handler.sendEmptyMessage(0);

    }

    @Override
    public void onCancel(Object o) {
        handler.removeMessages(0);
        handler = null;
        count = 0;
    }
}

其中onCancel代表對面不再接收,這裡我們應該做一些clean up的事情。而 onListen則代表通道已經建好,Native可以發送數據了。註意onListen裡帶的EventSink這個參數,後續Native發送數據都是經過EventSink的。

第二步:同MethodChannel一樣,發起通信請求

class _MyHomePageState extends State<MyHomePage> {
  // 創建 EventChannel
  static const stream = const EventChannel('com.example.flutter_battery/stream');

  int _count = 0;

  StreamSubscription _timerSubscription;

  void _startTimer() {
    if (_timerSubscription == null)
       // 監聽 EventChannel 流, 會觸發 Native onListen回調
      _timerSubscription = stream.receiveBroadcastStream().listen(_updateTimer);
  }

  void _stopTimer() {
    _timerSubscription?.cancel();
    _timerSubscription = null;
    setState(() => _count = 0);
  }

  void _updateTimer(dynamic count) {
    print("--------$count");
    setState(() => _count = count);
  }

  @override
  void dispose() {
    super.dispose();
    _timerSubscription?.cancel();
    _timerSubscription = null;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        margin: EdgeInsets.only(left: 10, top: 10),
        child: Center(
          child: Column(
            children: [
              Row(
                children: <Widget>[
                  RaisedButton(
                    child: Text('Start EventChannel',
                        style: TextStyle(fontSize: 12)),
                    onPressed: _startTimer,
                  ),
                  Padding(
                      padding: EdgeInsets.only(left: 10),
                      child: RaisedButton(
                        child: Text('Cancel EventChannel',
                            style: TextStyle(fontSize: 12)),
                        onPressed: _stopTimer,
                      )),
                  Padding(
                    padding: EdgeInsets.only(left: 10),
                    child: Text("$_count"),
                  )
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}

整體說明一下:Flutter端通過stream.receiveBroadcastStream().listen監聽native發送過來的數據,native端通過eventSink.success(++count)不斷的將數據返回給Flutter端,這樣就實現了我們想要的實時監聽的效果了!

  • BasicMessageChannel

其實他就是一個簡版的MethodChannel,也可以說MethodChannel是基於BasicMessageChannel實現的,BasicMessageChannel只是進行通信,更通俗的理解就是兩端發通知,但是不需要進行方法匹配。

第一步:初始化及註冊-native

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // 省略其他代碼...
    
    messageChannel = new BasicMessageChannel<>(flutterView, CHANNEL, StringCodec.INSTANCE);
    messageChannel.
        setMessageHandler(new MessageHandler<String>() {
            @Override
            public void onMessage(String s, Reply<String> reply) {
                // 接收到Flutter消息, 更新Native
                onFlutterIncrement();
                reply.reply(EMPTY_MESSAGE);
            }
        });

    FloatingActionButton fab = findViewById(R.id.button);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // 通知 Flutter 更新
            sendAndroidIncrement();
        }
    });
}

private void sendAndroidIncrement() {
    messageChannel.send(PING);
}

private void onFlutterIncrement() {
    counter++;
    TextView textView = findViewById(R.id.button_tap);
    String value = "Flutter button tapped " + counter + (counter == 1 ? " time" : " times");
    textView.setText(value);
}

第二步:Flutter端發起通信-flutter

class _MyHomePageState extends State<MyHomePage> {
  static const String _channel = 'increment';
  static const String _pong = 'pong';
  static const String _emptyMessage = '';
  static const BasicMessageChannel<String> platform =
      BasicMessageChannel<String>(_channel, StringCodec());

  int _counter = 0;

  @override
  void initState() {
    super.initState();
    // 設置消息處理器
    platform.setMessageHandler(_handlePlatformIncrement);
  }

  // 如果接收到 Native 的消息 則數字+1
  Future<String> _handlePlatformIncrement(String message) async {
    setState(() {
      _counter++;
    });
    // 發送一個空消息
    return _emptyMessage;
  }

  // 點擊 Flutter 中的 FAB 則發消息給 Native
  void _sendFlutterIncrement() {
    platform.send(_pong);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('BasicMessageChannel'),
      ),
      body: Container(
          child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Expanded(
            child: Center(
              child: Text(
                  'Platform button tapped $_counter time${_counter == 1 ? '' : 's'}.',
                  style: const TextStyle(fontSize: 17.0)),
            ),
          ),
          Container(
            padding: const EdgeInsets.only(bottom: 15.0, left: 5.0),
            child: Row(
              children: <Widget>[
                Image.asset('assets/flutter-mark-square-64.png', scale: 1.5),
                const Text('Flutter', style: TextStyle(fontSize: 30.0)),
              ],
            ),
          ),
        ],
      )),
      floatingActionButton: FloatingActionButton(
        onPressed: _sendFlutterIncrement,
        child: const Icon(Icons.add),
      ),
    );
  }
}

 

總結:以上就是Flutter和native通信的全部內容了,理解了以後其實很簡單,上面的內容有一些我個人的理解,更深一層的還需要繼續挖掘!


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 首先要認識大數據 什麼是大數據?可能有人會說寫字樓的所有人的資料信息就是個大數據。NO!這裡的數據只能說比較大,但卻不能稱之為大數據。百度百科上給出了很明確的解釋“大數據(big data),指無法在一定時間範圍內用常規軟體工具進行捕捉、管理和處理的數據集合,是需要新處理模式才能具有更強的決策力、洞 ...
  • 1.創建學生管理系統資料庫xscj create detabase 資料庫名; 2.打開資料庫 use 資料庫名; //創建資料庫之後,該資料庫不會自動成為當前資料庫需要用use來指定 3.創建表名 4.在表xs中增加“獎學金等級”列 ,並刪除表中的“姓名”列 alter table 表名 add ...
  • 轉自:http://www.maomao365.com/?p=10025 摘要: 下文使用sql腳本生成中文名字的方法分享,如下所示: 實驗環境:sql server 2008 R2 在工作中,我們有時需要批量生成隨機姓名,下麵將講述使用sql腳本生成隨機"名字"的方法分享,如下所示:實現思路: 1 ...
  • 前面的文章中我們講道,像趣頭條類的APP對於收徒和閱讀行為給予用戶現金獎勵的方式勢必會受到大量羊毛黨黑產的註意,其實單個用戶能薅到的錢是沒有多少的,為了達到利益最大化,黑產肯定會利用各種手段構建大量賬號來薅APP運營企業的羊毛,因為收徒的獎勵遠高於閱讀,所以賺取收徒獎勵就成了最嚴重的薅羊毛手段。前文 ...
  • 早上剛睜眼,看到了一堆資料庫告警的簡訊,其中一個內容如下: 眼看這是剛從其他DBA交接過來的資料庫,不敢怠慢,立馬起來查看從庫日誌信息如下: 即非正常停止。 再登錄主庫機器查看主庫錯誤日誌,信息如下 從主庫日誌可以看出,2個從庫是主庫主動斷開的,而給出的信息也指出了原因failed on flush ...
  • Microsoft SQL Server 2012安裝說明 環境:Windows8, Windows7, WinVista, Win2003, WinXP Microsoft SQL Server 2012是一款強大的MySQL資料庫管理和開發工具。新版的Microsoft SQL Server 2 ...
  • 1. Objective C語言使用的是"消息結構"而非"函數調用"。 "消息結構"和"函數調用"之間的區別 "消息結構"的語言: 運行時由運行環境決定所應執行的代碼 "函數調用"的語言: 由編譯器決定 記憶體模型:Objective C語言中的指針是用來指示對象的。 Objective C為C語言添 ...
  • flutterBoost使用筆記 新一代Flutter-Native混合解決方案。 FlutterBoost是一個Flutter插件,它可以輕鬆地為現有原生應用程式提供Flutter混合集成方案。FlutterBoost的理念是將Flutter像Webview那樣來使用。在現有應用程式中同時管理Na ...
一周排行
    -Advertisement-
    Play Games
  • 1. 說明 /* Performs operations on System.String instances that contain file or directory path information. These operations are performed in a cross-pla ...
  • 視頻地址:【WebApi+Vue3從0到1搭建《許可權管理系統》系列視頻:搭建JWT系統鑒權-嗶哩嗶哩】 https://b23.tv/R6cOcDO qq群:801913255 一、在appsettings.json中設置鑒權屬性 /*jwt鑒權*/ "JwtSetting": { "Issuer" ...
  • 引言 集成測試可在包含應用支持基礎結構(如資料庫、文件系統和網路)的級別上確保應用組件功能正常。 ASP.NET Core 通過將單元測試框架與測試 Web 主機和記憶體中測試伺服器結合使用來支持集成測試。 簡介 集成測試與單元測試相比,能夠在更廣泛的級別上評估應用的組件,確認多個組件一起工作以生成預 ...
  • 在.NET Emit編程中,我們探討了運算操作指令的重要性和應用。這些指令包括各種數學運算、位操作和比較操作,能夠在動態生成的代碼中實現對數據的處理和操作。通過這些指令,開發人員可以靈活地進行算術運算、邏輯運算和比較操作,從而實現各種複雜的演算法和邏輯......本篇之後,將進入第七部分:實戰項目 ...
  • 前言 多表頭表格是一個常見的業務需求,然而WPF中卻沒有預設實現這個功能,得益於WPF強大的控制項模板設計,我們可以通過修改控制項模板的方式自己實現它。 一、需求分析 下圖為一個典型的統計表格,統計1-12月的數據。 此時我們有一個需求,需要將月份按季度劃分,以便能夠直觀地看到季度統計數據,以下為該需求 ...
  • 如何將 ASP.NET Core MVC 項目的視圖分離到另一個項目 在當下這個年代 SPA 已是主流,人們早已忘記了 MVC 以及 Razor 的故事。但是在某些場景下 SSR 還是有意想不到效果。比如某些靜態頁面,比如追求首屏載入速度的時候。最近在項目中回歸傳統效果還是不錯。 有的時候我們希望將 ...
  • System.AggregateException: 發生一個或多個錯誤。 > Microsoft.WebTools.Shared.Exceptions.WebToolsException: 生成失敗。檢查輸出視窗瞭解更多詳細信息。 內部異常堆棧跟蹤的結尾 > (內部異常 #0) Microsoft ...
  • 引言 在上一章節我們實戰了在Asp.Net Core中的項目實戰,這一章節講解一下如何測試Asp.Net Core的中間件。 TestServer 還記得我們在集成測試中提供的TestServer嗎? TestServer 是由 Microsoft.AspNetCore.TestHost 包提供的。 ...
  • 在發現結果為真的WHEN子句時,CASE表達式的真假值判斷會終止,剩餘的WHEN子句會被忽略: CASE WHEN col_1 IN ('a', 'b') THEN '第一' WHEN col_1 IN ('a') THEN '第二' ELSE '其他' END 註意: 統一各分支返回的數據類型. ...
  • 在C#編程世界中,語法的精妙之處往往體現在那些看似微小卻極具影響力的符號與結構之中。其中,“_ =” 這一組合突然出現還真不知道什麼意思。本文將深入剖析“_ =” 的含義、工作原理及其在實際編程中的廣泛應用,揭示其作為C#語法奇兵的重要角色。 一、下劃線 _:神秘的棄元符號 下劃線 _ 在C#中並非 ...