1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| class EventChannelDemo(var activity: Activity, messenger: BinaryMessenger): EventChannel.StreamHandler { private var channel: EventChannel private var index = 0 private var events: EventChannel.EventSink? = null init { channel = EventChannel(messenger, "com.example.androidflutter.EventChannelDemo") channel.setStreamHandler(this) startTimer() } fun startTimer() { var timer = Timer().schedule(timerTask { index++ var map = mapOf("name" to "张三 ${index}", "age" to "${index}" ) activity.runOnUiThread { events?.success(map) }
}, 0, 1000)
} override fun onListen(arguments: Any?, events: EventChannel.EventSink?) { this.events = events }
override fun onCancel(arguments: Any?) { this.events = null } }
|