- 在合适的地方调用BasicMessageChannel
2.1 Flutter 端创建 MethodChannel 通道
代码
void main() => runApp(BasicMessageChannelDemo());
class BasicMessageChannelDemo extends StatefulWidget {
@override
_BasicMessageChannelDemoState createState() => _BasicMessageChannelDemoState();
}
class _BasicMessageChannelDemoState extends State {
var channel = BasicMessageChannel(‘com.example.androidflutter.BasicMessageChannelDemo’,StandardMessageCodec());
var _data;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text(“Flutter BasicMessageChannel”)),
body: Column(
children: [
SizedBox(height: 50),
RaisedButton(
child: Text(‘发送数据到原生’),
onPressed: () async {
var result = await channel.send({‘name’: ‘张三’, ‘age’: 18}) as Map;
var name = result[‘name’];
var age = result[‘age’];
setState(() {
_data = ‘ n a m e , name, name,age’;
});
},
),
Text(‘原生返回数据:$_data’),
],
),
),
);
}
}
说明
-
com.example.androidflutter.BasicMessageChannelDemo:为通信Channel的全类名
-
channel.send({‘name’: ‘张三’, ‘age’: 18}):为通信数据
2.2 Android 端创建 MethodChannel 通道
代码
class BasicMessageChannelDemo(messenger: BinaryMessenger) : BasicMessageChannel.MessageHandler {
private var channel: BasicMessageChannel
init {
channel = BasicMessageChannel(messenger, “com.example.androidflutter.BasicMessageChannelDemo”, StandardMessageCodec())
channel.setMessageHandler(this)
}
override fun onMessage(message: Any?, reply: BasicMessageChannel.Reply) {
val name = (message as Map<String, Any>)[“name”]
val age = (message as Map<String, Any>)[“age”]
var map = mapOf(“name” to “hello,$name”,
“age” to “$age”
)
reply.reply(map)
}
}
2.3 调用处
class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
BasicMessageChannelDemo(flutterEngine.dartExecutor.binaryMessenger)
}
}
2.4 效果图
3.1 Flutter端
void main() => runApp(BasicMessageChannelDemo());
class BasicMessageChannelDemo extends StatefulWidget {
@override
_BasicMessageChannelDemoState createState() => _BasicMessageChannelDemoState();
}
class _BasicMessageChannelDemoState extends State {
var channel = BasicMessageChannel(‘com.example.androidflutter.BasicMessageChannelDemo’,StandardMessageCodec());
var _data;
var _nativeData;
@override
void initState() {
super.initState();
channel.setMessageHandler((message) async {
setState(() {
_nativeData = (message as Map)[‘count’];
});
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text(“Flutter BasicMessageChannel”)),
body: Column(
children: [
SizedBox(height: 50),
RaisedButton(
child: Text(‘发送数据到原生’),
onPressed: () async {
var result = await channel.send({‘name’: ‘张三’, ‘age’: 18}) as Map;
var name = result[‘name’];
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
最后
下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。希望能够帮助到大家提升技术
高级UI,自定义View
UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。
不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
mg-ZbwlrLcW-1712636427721)]
高级UI,自定义View
UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。
不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!
[外链图片转存中…(img-EaW5M0Nm-1712636427721)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!