Skip to content

Commit

Permalink
添加BasicMessageChannel
Browse files Browse the repository at this point in the history
  • Loading branch information
yanchunlan committed Aug 2, 2023
1 parent cca5dbe commit a209541
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.flutter.plugin.common.*
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory
import io.flutter.plugins.GeneratedPluginRegistrant
import java.util.*
import kotlin.random.Random


Expand All @@ -27,6 +28,8 @@ class MainActivity : FlutterActivity() {
super.onCreate(savedInstanceState)
registerMethodChannel()
registerMethodChannel1()
registerEventChannel()
registerBasicMessageChannel()
}

override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
Expand All @@ -38,7 +41,7 @@ class MainActivity : FlutterActivity() {
super.onPause()
Log.d(TAG, "onPause: ")

// 原生调用flutter代码
// 1. methodChannel - 原生调用flutter代码
methodChannel.invokeMethod("getFlutterInfo", null, object : MethodChannel.Result {
override fun success(result: Any?) {
Log.d(TAG, "onPause: getFlutterInfo success $result")
Expand All @@ -54,9 +57,9 @@ class MainActivity : FlutterActivity() {
})


// 原生主动发送事件到flutter
// 2. EventChannel - 原生主动发送事件到flutter
var eventSink: EventChannel.EventSink? = null
val eventChannel = EventChannel(flutterEngine?.dartExecutor, "example.native_method.eventChannel/test")
val eventChannel = EventChannel(flutterEngine?.dartExecutor?.binaryMessenger, "example.native_method.eventChannel/test")
eventChannel.setStreamHandler(object: EventChannel.StreamHandler {
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
eventSink = events
Expand All @@ -67,6 +70,21 @@ class MainActivity : FlutterActivity() {
eventSink?.success(100)
}

// 3. BasicMessageChannel - 原生调用flutter
private fun registerBasicMessageChannel() {
val manager = flutterEngine?.dartExecutor?.binaryMessenger
if(manager != null){
val messageChannel = BasicMessageChannel<String>(manager, "example.native_method.basicMessageChannel/test", StringCodec.INSTANCE);
messageChannel.send("BasicMessageChannelPlugin message test") { reply ->
// reply?.reply("BasicMessageChannel收到:") //可以通过reply进行回复
Log.d(TAG, "BasicMessageChannelPlugin reply BasicMessageChannel收到$reply")
}
}
}

private fun registerEventChannel() {

}

private fun registerMethodChannel1() {
val registrar = ShimPluginRegistry(flutterEngine!!)
Expand All @@ -76,7 +94,7 @@ class MainActivity : FlutterActivity() {
registrar.platformViewRegistry().registerViewFactory("SampleView", playerViewFactory)
}

// flutter 调用原生代码
// 1. methodChannel - flutter 调用原生代码
private fun registerMethodChannel() {
val manager = flutterEngine?.dartExecutor?.binaryMessenger
if(manager != null){
Expand Down
8 changes: 7 additions & 1 deletion lib/jike/07_dart_async.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:isolate';
import 'package:isolate/isolate.dart';

void main() async {
print(".......sample1.......");
Expand All @@ -23,11 +25,15 @@ void main() async {
print(await asyncFactoriali(4));
//await Future.delayed(const Duration(seconds: 1), () => print(".......sample9......."));
//compute函数仅能在Flutter工程中使用
//print(await compute(syncFactorial, 4));
// print(await compute(syncFactorial, 4));

Future<LoadBalancer> loadBalancer = LoadBalancer.create(2, IsolateRunner.spawn);
final lb = await loadBalancer;
int res = await lb.run<int, int>(syncFactorial, 1);
}



sample1() {
Future(() => print('f1'));
Future(() => print('f2'));
Expand Down
1 change: 1 addition & 0 deletions lib/jike/10_native_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class NativeMethodLessWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
TimeUtils.eventChannelListener();
TimeUtils.basicMessageChannelListener();

return Scaffold(
backgroundColor: Colors.yellowAccent,
Expand Down
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ Widget jikeWidget() {
// return AnimationPage();
// return NetworkPage();
// return PersistencePage();
// return NativeMethodLessWidget();
return NativeMethodLessWidget();
// return NativeViewPage();
// return ProviderPage();
// return ScreenPage();
return LogPage();
// return LogPage();
// return ModulePage();
}

Expand Down
13 changes: 13 additions & 0 deletions lib/utils/timeUtils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,17 @@ class TimeUtils{
});
}

static const BasicMessageChannel<String> _basicMessageChannel = BasicMessageChannel('example.native_method.basicMessageChannel/test', StringCodec());
static void basicMessageChannelListener() async {
//监听原生端发送过来的事件
_basicMessageChannel.setMessageHandler((message){
return Future<String>(() {
print("TimeUtils.basicMessageChannelListener setMessageHandler $message");
return "basicMessageChannelListener setMessageHandler $message"; });
});
//向原生端发送消息
var r = await _basicMessageChannel.send("flutter BasicMessageChannel");
print("TimeUtils.basicMessageChannelListener send");
}

}
13 changes: 10 additions & 3 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ packages:
name: built_value
url: "https://pub.flutter-io.cn"
source: hosted
version: "8.5.0"
version: "8.6.0"
cached_network_image:
dependency: "direct main"
description:
Expand Down Expand Up @@ -370,7 +370,7 @@ packages:
name: fluttertoast
url: "https://pub.flutter-io.cn"
source: hosted
version: "8.2.1"
version: "8.2.2"
frontend_server_client:
dependency: transitive
description:
Expand Down Expand Up @@ -434,6 +434,13 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.4"
isolate:
dependency: "direct main"
description:
name: isolate
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.1"
js:
dependency: transitive
description:
Expand Down Expand Up @@ -559,7 +566,7 @@ packages:
name: path_provider_linux
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.10"
version: "2.1.11"
path_provider_platform_interface:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies:
crypto: ^3.0.2 # md5加密
logger: ^1.1.0
dog: ^1.4.0

isolate: ^2.0.2

# 中台化,尽量使用 project -> module -> plugin ,其中module相当于flutter的中间层
crash:
Expand Down

0 comments on commit a209541

Please sign in to comment.