Flutter與原生如何進(jìn)行通信

??用Flutter這樣的跨平臺(tái)技術(shù)進(jìn)行商業(yè)級(jí)項(xiàng)目開(kāi)發(fā)時(shí),幾乎不可避免的需要和Native進(jìn)行通信,比如不同平臺(tái)的底層服務(wù)如電量變化、網(wǎng)絡(luò)連接變化、陀螺儀、傳感器等等都有各自不同的實(shí)現(xiàn),以及編譯期的一些配置,比如包名、版本號(hào)、第三方依賴APPKEY等都需要通過(guò)原生的方法去獲取。所以得學(xué)一哈Native和Flutter的通信方式是非常有必要的

前話

Native和Flutter之間可以通過(guò)Platform Channels APIs進(jìn)行通信,
Flutter定義了三種不同類型的Channel:


  • MethodChannel:用于傳遞方法調(diào)用(method invocation),適用于一次性通信

  • BasicMessageChannel:用于傳遞字符串和半結(jié)構(gòu)化的消息,持續(xù)通信可回復(fù)

  • EventChannel: 用于事件流的發(fā)送(event streams),持續(xù)通信不可回復(fù)

一、MethodChannel通信

MethodChannel是最常用的Native和Flutter的通信方式,主要用于Flutter調(diào)用Native端方法,如調(diào)用Native相機(jī)功能

為了便于理解,這里我介紹一個(gè)前兩天在公司項(xiàng)目中實(shí)際用到MethodChannel的場(chǎng)景?;粳F(xiàn)在Android APP都會(huì)涉及到多渠道打包問(wèn)題,針對(duì)不同的渠道,我們可能會(huì)在編譯腳本(build文件)中進(jìn)行一些不同的配置。但是Flutter如何才能拿到Android Gradle中的配置信息呢?其實(shí)很簡(jiǎn)單,也就是我們可以先通過(guò)Android代碼拿到BuildConfig中的數(shù)據(jù),再通過(guò)Flutter與Android進(jìn)行通信拿到數(shù)據(jù)。這種一次性的調(diào)用Android中代碼的情況就需要用到MethodChannel

1.1 Android端

創(chuàng)建MethodChannel,通過(guò)setMethodCallHandler接收Flutter端的方法調(diào)用

    private val Method_Channel = "com.example.flutter_test_call/methodChannel"
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        //1.創(chuàng)建android端的MethodChannel
        val methodChannel = MethodChannel(flutterEngine.dartExecutor, Method_Channel)
        //2.通過(guò)setMethodCallHandler響應(yīng)Flutter端的方法調(diào)用
        methodChannel.setMethodCallHandler { call, result ->
            //判斷調(diào)用的方法名
            if(call.method.equals("testMethodCall")){
                //獲取傳遞的參數(shù)
                Log.e("test", "Have received Test Method Call :${call.arguments}")
                //返回結(jié)果給Flutter端
                result.success(BuildConfig.DEBUG.toString() + " " +"yes")
            }
        }
    }
1.2 Flutter端

Flutter端同樣也有MethodChannel,可以通過(guò)類似方法調(diào)用的方式,調(diào)用Native層的方法,并拿到返回值

  String Method_Channel = "com.example.flutter_test_call/methodChannel";
  Future<void> testMethodCall() async {
    //1.創(chuàng)建Flutter端的MethodChannel
    MethodChannel _methodChannel = MethodChannel(Method_Channel);
    //2.通過(guò)invokeMethod調(diào)用Native方法,拿到返回值
    String debugString = await _methodChannel.invokeMethod("testMethodCall", "give me debug info");
    print('test debugString=$debugString');
  }

這里要注意兩點(diǎn):

  • ChannelName要與Native端保持一致(即com.example.flutter_test_call/methodChannel)
  • MethodName要與Native端保持一致(即testMethodCall)

二、BasicMessageChannel通信

BasicMessageChannel用于Native和Flutter互相發(fā)送消息,一方給另一方發(fā)送消息,收到消息之后給出回復(fù)

2.1 Android給Flutter發(fā)送消息
2.1.1 Android端

創(chuàng)建一個(gè)BasicMessageChannel,通過(guò)send方法發(fā)送消息

private val Basic_Method_Channel = "com.example.flutter_test_call/basicMethodChannel"

override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
    super.configureFlutterEngine(flutterEngine)
    //1.創(chuàng)建android端的BasicMessageChannel
    val basicMethodChannel = BasicMessageChannel(flutterEngine.dartExecutor, Basic_Method_Channel, StringCodec.INSTANCE)
    //2.向Flutter端發(fā)送消息
    basicMethodChannel.send("Hello, this is BasicMethodChannel Send msg!") {         
            replyString ->            
               Log.i("test", "收到了Flutter端發(fā)來(lái)的回復(fù):$replyString")
    }
}

發(fā)送的消息會(huì)以二進(jìn)制的形式進(jìn)行處理,所以要針對(duì)不同類型的數(shù)據(jù)進(jìn)行二進(jìn)制編碼

編碼類型 消息格式
BinaryCodec 發(fā)送二進(jìn)制消息
JSONMessageCodec 發(fā)送Json格式消息
StandardMessageCodec 發(fā)送基本型數(shù)據(jù)
StringCodec 發(fā)送String類型消息
2.1.2 Flutter端

Flutter端同樣也有BasicMessageChannel,通過(guò)setMessageHandler接收并回復(fù)消息

String Basic_Method_Channel = "com.example.flutter_test_call/basicMethodChannel";
initBasicMethodCall(){
  //1.創(chuàng)建Flutter端的BasicMessageChannel
  basicMessageChannel = BasicMessageChannel(Basic_Method_Channel, StringCodec());
  //2.接收來(lái)自Native的消息,并向Native回復(fù)
  basicMessageChannel.setMessageHandler((message) {
    print('test Android端發(fā)來(lái)的消息:$message');
    return Future.value('黃河黃河,我是長(zhǎng)江');
  });
}
2.2 Flutter給Android發(fā)送消息
2.2.1 Flutter端

在Flutter端我們也可以通過(guò)send方法向Native發(fā)送消息,方法的返回值就是Native端的消息回復(fù)

注意flutter和Native的通信都是異步的

testBasicMethodCall() async {
  String replyString =  await basicMessageChannel?.send("Android, 我是Flutter端!");
  print('test Android端回復(fù)的的消息:$replyString');
}
2.2.2 Android端

android端通過(guò)setMessageHandler設(shè)置消息處理器,處理來(lái)自Dart的消息,收到消息后通過(guò)reply進(jìn)行回復(fù)

    basicMethodChannel.setMessageHandler { message, reply ->
        Log.v("test", "收到了Flutter端發(fā)來(lái)的消息:$message")
        //通過(guò)reply進(jìn)行回復(fù)
        reply.reply("長(zhǎng)江長(zhǎng)江,我是黃河~")
    }

三、EventChannel通信

EventChannel用于從Native向Flutter發(fā)送通知事件,例如Flutter通過(guò)其監(jiān)聽(tīng)Android的重力感應(yīng)變化等。與MethodChannel不同,EventChannel是Native到Flutter的單向調(diào)用,調(diào)用是一對(duì)多的,類似Android的BrodcastReceiver

3.1 Android端

創(chuàng)建一個(gè)EventChannel,在StreamHandler#onLister回調(diào)中獲取EventSink引用并保存,當(dāng)重力感應(yīng)發(fā)送變化時(shí),通過(guò)eventSink.success向Flutter端發(fā)送消息

private val Event_Channel = "com.example.flutter_test_call/eventChannel"
var eventSink: EventChannel.EventSink? = null
fun registerEventChannel(binaryMessenger: BinaryMessenger){
     //1.創(chuàng)建Android端的EventChannel
    val eventChannel = EventChannel(binaryMessenger, Event_Channel)
    //2.在StreamHandler#onLister回調(diào)中獲取EventSink引用并保存
    eventChannel.setStreamHandler(object : EventChannel.StreamHandler{
        override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
            eventSink = events
        }

        override fun onCancel(arguments: Any?) {
            eventSink = null
        }

    })
}

fun getNetWorkType(context : Context){
    var connectivityManager : ConnectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

    var info = connectivityManager.activeNetworkInfo
    if(info != null){
        if(info?.type == ConnectivityManager.TYPE_WIFI){
            //3.調(diào)用eventSink.success向Flutter端發(fā)送消息
            eventSink?.success("WIFI")
        }else if(info.type == ConnectivityManager.TYPE_MOBILE){
            eventSink?.success("Mobile")
        }else{
            eventSink?.success("Unkonw")
        }
    }else{
        //3.報(bào)錯(cuò)后調(diào)用eventSink.error向Flutter端發(fā)送消息
        eventSink?.error("error network", "error network", "error network")
    }

}
3.2 Flutter端

Flutter端接收消息如下所示,要注意使用EventChannel時(shí)需要在頁(yè)面銷毀時(shí)取消監(jiān)聽(tīng),防止內(nèi)存泄漏

String eventChannel = 'com.example.flutter_test_call/eventChannel';
StreamSubscription streamSubscription;
testEventChannel(){
  //1.創(chuàng)建Flutter端EventChannel
  EventChannel _eventChannel = EventChannel(eventChannel);
  //2.EventChannel#receiveBroadcastStream注冊(cè)listener,建立監(jiān)聽(tīng)
  streamSubscription = _eventChannel.receiveBroadcastStream()
    .listen((event) {
      print("Network Status : $event");
  }, onError: (errorcode){
     print('errorcode: $errorcode');
  });
}

@override
void dispose() {
  // TODO: implement dispose
  super.dispose();
  //3.頁(yè)面銷毀時(shí)需要取消監(jiān)聽(tīng),防止內(nèi)存泄漏
  streamSubscription?.cancel();
}

申明:禁用于商業(yè)用途,如若轉(zhuǎn)載,請(qǐng)附帶原文鏈接。http://www.itdecent.cn/p/e376574e2402蟹蟹~

PS: 寫(xiě)文不易,覺(jué)得沒(méi)有浪費(fèi)你時(shí)間,請(qǐng)給個(gè)關(guān)注和點(diǎn)贊~ ??

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容