Flutter和原生交互---EventChannel

Posted 泸沽烟火

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flutter和原生交互---EventChannel相关的知识,希望对你有一定的参考价值。

EventChannel,用于android原生事件流向Flutter端的发送,例如通过原生监听重力感应等状态变化后向Flutter发送通知,一对多通知,类似于原生广播

原生端实现代码:

1.实现EventChannel:单例类,通过sendEventData通知Flutter

package com.example.flutter_event_channel_plugin;

import android.util.Log;

import androidx.annotation.NonNull;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.EventChannel;

public class FlutterEventChannel implements FlutterPlugin, EventChannel.StreamHandler {
    public static final String CHANNEL_NAME = "event_channel_name";
    private EventChannel eventChannel;
    private EventChannel.EventSink eventSink;

    private static volatile FlutterEventChannel instance;

    private FlutterEventChannel () {
    }

    public static FlutterEventChannel getInstance() {
        if (instance == null) {
            synchronized (FlutterEventChannel .class) {
                if (instance == null) {
                    instance = new FlutterEventChannel ();
                }
            }
        }
        return instance;
    }

    /**
     * 处理设置事件流的请求。
     *
     * @param arguments 流配置参数,可能为空。
     * @param events    用于向flutter接收器发射事件。
     */
    @Override
    public void onListen(Object arguments, EventChannel.EventSink events) {
        this.eventSink = events;
    }

    /**
     * 处理请求以删除最近创建的事件流。
     *
     * @param arguments 流配置参数,可能为空。
     */
    @Override
    public void onCancel(Object arguments) {

    }

    public void sendEventData(Object data) {
        if (eventSink != null) {
            eventSink.success(data);
        }
    }

    @Override
    public void onAttachedToEngine(@NonNull FlutterPlugin.FlutterPluginBinding binding) {
        eventChannel = new EventChannel(binding.getBinaryMessenger(), CHANNEL_NAME);
        eventChannel.setStreamHandler(this);
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding binding) {
        eventSink.endOfStream();
        eventChannel.setStreamHandler(null);
    }
}

2.注册通信类:需要注意的是这里的FlutterActivity是io.flutter.embedding.android包下的

FlutterEventChannel 

package com.example.example;

import android.os.Bundle;

import com.example.FlutterEventChannel;

import io.flutter.embedding.android.FlutterActivity;

public class MainActivity extends FlutterActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        FlutterEventChannel eventChannel = FlutterEventChannel.getInstance();
        getFlutterEngine().getPlugins().add(eventChannel);
    }
}

3.Flutter中注册并调用

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  var _eventChannel = EventChannel('event_channel_name');

  @override
  void initState() {
    super.initState();
    _eventChannel.receiveBroadcastStream().listen((event) {
      print("event: $event");
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
      ),
    );
  }
}

 

以上是关于Flutter和原生交互---EventChannel的主要内容,如果未能解决你的问题,请参考以下文章

Flutter和原生交互---EventChannel

Flutter和原生交互---EventChannel

Flutter和原生交互---EventChannel

Flutter与原生交互

Flutter和原生交互---StreamController.broadcast

Flutter和原生交互---StreamController.broadcast