Flutter框架分析-BasicMessageChannel

1.前语

在文章Flutter结构分析(八)-Platform Channel中,我们分析了BasicMessageChannel的原理和结构java怎样读,并详细说明了与其相关的一些中心类,例如MessageHandlerMessageCodec等,本文首要说明运用BasicMessageChannel的示例。

2.运用流程

BasicMessageChannel可用于Flutternative发音讯,也可用于nativeFlutter发音讯,所以接下来将别离分析这两种运用流程。

2.1 Flutter给android是什么手机牌子native发音讯

流程如下:

1)native端创立某flutter是什么意思channel naandroid手机me的BasicMessFlutterageChannelandroid平板电脑价格

2)native端运用setMessageHandler函数,设置该Basjson文件是干什么的icMessageChannelMessageHandler

3)Flutjsonter端创立该channel name的BasicMesflutter是什么意思sageChannel

4)Flutterandroid手机运用该BasicMessageChannel经过senandroid平板电脑价格d函数向native端发送音讯。

5)natijava怎样读ve端刚刚注册的MessageHandler收到发送的音讯,在onMessage中处理音讯,经过reply函数进行回复。

6)Flutter端处理该回复。

Flutter端要害代码如下:

class_MessageChanjava工作培训班nelStateextendsState<MessageChannelWidget> {
static const _cflutter中文官网hannel =BasicMessageChannel('flutter2/testmessagechannejson格局l',JSONMessageCodec());
inti=0;
void_sendMessage()asyjsonnc{
fiappearnalString reply=await _cjava言语hannjsonpel.sjsonpend(android体系'android手机Hjava难学吗ello World i:$i');
print('MessageChannelTestandroid电子市场 in dart$reply');
setState(() {
i++;
});
}
@override
Widgetbuild(java初学BuildContappreciateextcontext) {
rflutter言语eturnScaffold(
appBar:AppBar(
title:Text("message channel tflutter值得学吗est"),flutter框架
),
bojava初学dy:Center(
 // Center is a layout wiandroid电子市场dget. It takes a single child anjava面试题d positions it
// in theapple middle of the parent.
child:Coluandroid/yunosmn(
mainAxisAlignment:MainAxisAlignment.center,
children: <Widget>[
Text(
'You haveapprove pushedjava怎样读 the button this many times:',
),java模拟器
Text(
'$i',
style:Theme.*of*(context).textTheme.headline4,
),
],
),
),
floatingActijava怎样读onButton:FloatingActionButton(
onPrapp是什么意思essed:_sendMessage,
tooltip:'Increment',
child:Icon(Icons.approveadd),
), // This trailflutter框架优缺点ing comma makes auto-formatting nicer for build meflutter是什么意思thods);
}
}

natjson接口回来过错ive端要害代码如下:

classMessjava工作培训班ageChannjson数据elActivity: FluttapproveerActivity() {
companion object{
funstartActiviJavaty(activity:Activity) {
valintent= Intflutter是什么意思ent(activity,MessageChannelActivity::class.java)
activity.startActivity(intent)
}
}
override funconfigureFlutterEngine(@json格局怎样翻开NonNullflutterEngine:Fluttejava怎样读rEngine) {
Log.d("MessageChannelTest","MessageChapproveannelActivity configureFlutterEngine")
valchannel= BasicMessageChannel(
flutterEngine.dartExecutor.binaryMessenger,
"flutter2/testmessagechannel",
JSONMessageCodec.INSTANCE)
 // Rjava初学eceiappreciateve messages from Dart
channel.sandroid是什么手机牌子etMessageHandler {message, repjson接口回来过错ly->
Log.d("MessageChajsonnnelTest","in android Received message =$message")
reply.reply("Reply from Android")
}
}
}

2.2 native给Flutter发音讯

流程如下:

1)Flutter端创立该channel name的BasicMessageChannel

2)Flutter端运用setMesjson文件是干什么的sageHandler函数,设置该BasicMessageChannelHandler函数。

3)native端创立某channel name的BasicMessageChannel

4)native端运用该BasicMessjava难学吗ageChanjava开发nel经过send函数向Flutter端发送音讯。

5)Flutter端刚刚注册的Handler收到发送的音讯,并处理音讯,然后经过reply函数进行回复。java怎样读

6)native端处理该回复。

Flutandroid/yunoster端要害代码如下:

class_MessageChannelStateextendsState<MessageChannelWidget> {
static const _channel =BasicMessjson文件是干什么的ageChannel('fjava模拟器lutter2/flutter是什么意思testmessagechannel',java难学吗JSONMessageCodec());
intijson接口回来过错=0;
void_sendMessage()async{
finalString reply=await _flutter框架channel.send('Hello World i:$i');
print('MessageChannelTest in dart$reply');
setState(() {
i+json格局+;
});
}json
@json字符串override
voidinitState() {
 //app是什么意思 Rfluttershyeceive messages from platform
_chappearanceanneAndroidl.setMessageHandler((dynamicmessage)async{
print('MessageChannelandroid下载Test in dart Received messaandroid是什么手机牌子ge =$message');
return'Reply from Dart';
});
super.initState();
}
}

native端要害代码如下:

classMessageChannelActivity: FlutterActivity() {
companion object{android下载安装
funstartActivityflutter值得学吗(activity:Activity) {
valintent= Iflutter言语ntapproveent(activity,MessageChaandroid手机nnelActivity::class.java)
activity.startActivity(intent)
}
}
override funcJSONonfigureFlutterEngine(@NonNullflutterEngine:Fluttejava言语rEngine) {
Log.d("MessflutteringageChannelTest","MessageChannelActivity configureFlutterEngine")
valchannel= BasicMessageChannel(
flutterEngijava言语ne.dartExecutor.binaryMessenger,
"flutter2/testmessagechannel",
JSONMessageCodec.INSTANCE)
// Send message to Dart
Handler().postDelayed(json格局 {
channel.send("Hello World from Android") { reply->
Log.d("MessageChannelTest","in android$reply")
 }
},500)
}
}

3. 小结

本文首要介绍了BasicMessageChannel的运用流程,并排举了一个运用BasicMessageChannel的示例。

发表评论

提供最优质的资源集合

立即查看 了解详情