Flutter与Native通信的坑

private EventChannel eventChannel = null;
private EventChannel.EventSink listener = null;

eventChannel = new EventChannel(binding.getBinaryMessenger(),"xxx");
        eventChannel.setStreamHandler(new EventChannel.StreamHandler() {
            @Override
            public void onListen(Object arguments, EventChannel.EventSink events) {
                if(events != null){
                    listener = events;
                    Log.i("zero","*******onListen= " + events);
                }
            }

            @Override
            public void onCancel(Object arguments) {
                listener = null;
                Log.i("zero","********onCancel= " + arguments);
            }
        });

public boolean onXXX(boolean flag) {
                Log.i("bugly_event", (isNativeCrashed ? "onCrashHandleEnd Native" : "XXX") + " handle end Thread=" + Thread.currentThread().getName());
                if (listener != null) {
                    Log.d("bugly_event", "onCrashHandleEnd: requestKey crash上报");
                    handler.postAtFrontOfQueue(new Runnable() {
                        @Override
                        public void run() {
                            Log.e("bugly_event","xxx:*********** " + Thread.currentThread().getName());
                            listener.success("xxx");//注意 EventChannel.EventSink必须在UI主线程调用
                        }
                    });
                }
                return true;
            }

Tips:
EventChannel.EventSink的方法 必须在UI主线程调用,不然Flutter端接收不到消息

你可能感兴趣的:(Flutter与Native通信的坑)