flutter与原生activity 交互 数据传递(一)

在这里插入图片描述

flutter/kotlin/Android 项目

官方代码: https://github.com/flutter/flutter/tree/master/examples/flutter_view

flutter入门(推荐):
https://github.com/alibaba/flutter-common-widgets-app

在这里插入图片描述

重点

MainAcvity

  • 进入Android MainActivity 修改继承,MainActivity() : Activity()
  • manifest文件中,需改mainActiity主题
 android:theme="@style/Theme.AppCompat"
 <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/Theme.AppCompat"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- This keeps the window background of the activity showing
                 until Flutter renders its first frame. It can be removed if
                 there is no splash screen (such as the default splash screen
                 defined in @style/LaunchTheme). -->
            <meta-data
                android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                android:value="true" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

FlutterMain.ensureInitializationComplet初始化flutterMain,保证flutterView可用,在setContentView配置layout之前

  • 绑定flutter运行路径,以及入口
 		var flutterRunArguments = FlutterRunArguments()
        //绑定路径
        flutterRunArguments.bundlePath = FlutterMain.findAppBundlePath(applicationContext)
        //获取入口 main方法
        flutterRunArguments.entrypoint = "main"
  • 设置消息频道,注意,第二个参数是频道,一定不能错。可用理解Android中广播,错误将无法接收传递
    android_channel
messageChannel = BasicMessageChannel(flutterView, "android_channel", StringCodec.INSTANCE)  
  • 发送消息,这里为flutter接受的数据,传递
messageChannel?.send("from android message")
  • 接收处理传递过来的消息
 messageChannel?.setMessageHandler(object : BasicMessageChannel.MessageHandler<String> {
   
            override fun onMessage(p0: String?, p1: BasicMessageChannel.Reply<String>?) {
   
                print("BasicMessageChannel")
                //处理来自 flutter  main里面的消息
                onFlutterMessage();
                p1?.reply(EMPTY_MESSAGE);

            }

        })

flutter main中

  • 设置频道,于上面的android_channel对应
 static const BasicMessageChannel<String> platform =
      BasicMessageChannel<String>("android_channel", StringCodec());
  • 接收消息
 //接收到Android activity发送到消息  处理消息
  @override
  void initState() {
   
    super.initState();
    platform.setMessageHandler(_handlePlatformMessage); //获取到消息处理方法
  }
  
  Future<String> _handlePlatformMessage
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值