美文网首页
FlutterBoost 之 注册自己的MethodChanne

FlutterBoost 之 注册自己的MethodChanne

作者: 弘法大师 | 来源:发表于2020-05-11 16:40 被阅读0次

    解决方案

    在FlutterBoost回调中,注册MethodChannel。

    val router = INativeRouter { context, url, urlParams, requestCode, exts ->
                val assembleUrl = Utils.assembleUrl(url, urlParams)
                FlutterPageRouter.openPageByUrl(context, assembleUrl, urlParams)
            }
    
            val boostLifecycleListener = object : FlutterBoost.BoostLifecycleListener {
    
                override fun beforeCreateEngine() {
                    IKLog.i("FlutterComponent beforeCreateEngine")
                }
    
                override fun onEngineCreated() {
                    IKLog.i("FlutterComponent onEngineCreated")
                   MethodChannel(FlutterBoost.instance().engineProvider().dartExecutor, "").setMethodCallHandler(...)
                }
    
                override fun onPluginsRegistered() {
                    IKLog.i("FlutterComponent onPluginsRegistered")
                }
    
                override fun onEngineDestroy() {
                    IKLog.i("FlutterComponent onEngineDestroy")
                    FlutterPluginManager.release()
                }
    
            }
    
            //
            // AndroidManifest.xml 中必须要添加 flutterEmbedding 版本设置
            //
            //   <meta-data android:name="flutterEmbedding"
            //               android:value="2">
            //    </meta-data>
            // GeneratedPluginRegistrant 会自动生成 新的插件方式 
            //
            // 插件注册方式请使用
            // FlutterBoost.instance().engineProvider().getPlugins().add(new FlutterPlugin());
            // GeneratedPluginRegistrant.registerWith(),是在engine 创建后马上执行,放射形式调用
            //
    
            val platform = FlutterBoost.ConfigBuilder(application, router)
                    .isDebug(BuildConfig.DEBUG)
                    .whenEngineStart(FlutterBoost.ConfigBuilder.FLUTTER_ACTIVITY_CREATED)
                    .renderMode(FlutterView.RenderMode.texture)
                    .lifecycleListener(boostLifecycleListener)
                    .build()
            FlutterBoost.instance().init(platform)
    

    解决思路

    在继承FlutterBoost之前,我们注册MethodChannel通常是采用继承io.flutter.app.FlutterFragmentActivity,然后拿到FlutterView

    EventChannel(flutterActivity.flutterView, mName).setStreamHandler(object: EventChannel.StreamHandler{
                override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
                }
    
                override fun onCancel(arguments: Any?) {
                }
            })
    

    继承FlutterBoost之后,需要使用boost提供的BoostFlutterActivity,但查看源码并没有FlutterView

    解决这个问题之前,我们先看下MethodChannel的源码。

    public MethodChannel(BinaryMessenger messenger, String name) {
            this(messenger, name, StandardMethodCodec.INSTANCE);
        }
    

    看见初始化MethodChannel第一个参数为BinaryMessenger

    BinaryMessenger: 使用与二进制消息一起传递的异步消息与Flutter通信的工具。
    我们Flutter提供的FlutterView,其实就是BinaryMessenger

    public class FlutterView extends SurfaceView implements BinaryMessenger, TextureRegistry 
    

    拿我们继续看看这个FlutterView实现了BinaryMessenger的方法做了什么。

        public void send(String channel, ByteBuffer message, BinaryReply callback) {
            if (!isAttached()) {
                Log.d(TAG, "FlutterView.send called on a detached view, channel=" + channel);
                return;
            }
            mNativeView.send(channel, message, callback);
        }
    

    继续追mNativeView的源码

        @Override
        @UiThread
        public void send(String channel, ByteBuffer message, BinaryReply callback) {
            if (!isAttached()) {
                Log.d(TAG, "FlutterView.send called on a detached view, channel=" + channel);
                return;
            }
    
            dartExecutor.getBinaryMessenger().send(channel, message, callback);
        }
    

    由此可见,其实是这个darExecutor在起作用。
    再看DartExecutor源码

    public class DartExecutor implements BinaryMessenger
    

    是不是恍然大悟,那么只需要从FlutterBoost中拿到DartExecutor即可。

    FlutterBoost.instance().engineProvider().dartExecutor
    

    也就是说这个darExecutor已经在FLutterBoost中了,在Engine初始化后就产生了。

    IOS解决方案

    思路同Android一致

    PlatformRouterImp *router = [PlatformRouterImp new];
        [FlutterBoostPlugin.sharedInstance startFlutterWithPlatform:router
                                                            onStart:^(FlutterEngine *engine) {
            [FlutterMethodChannel methodChannelWithName:@"the.bridge" binaryMessenger:engine.binaryMessenger];
                                                            }];
    

    相关文章

      网友评论

          本文标题:FlutterBoost 之 注册自己的MethodChanne

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