美文网首页Dart & Flutter 相关
Flutter与Native通信的坑

Flutter与Native通信的坑

作者: 小乌龟爸 | 来源:发表于2021-04-01 11:11 被阅读0次
    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通信的坑

        本文链接:https://www.haomeiwen.com/subject/osqtkltx.html