一、角色设定
- 发布者:MainActivity
- 订阅者:FirstActivity、SecondActivity、ThirdActivity
二、代码实现
-
定义全局 Channel
object MessageChannel { val channel = Channel<Int>() }
注:MessageChannel.channel 是一个 Channel 对象,用于在不同页面之间传递整数类型的计数值。它充当了一个中介,允许一个页面发送计数值,而其他页面则可以接收这些计数值。
-
发布者 MainActivity
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) startTimer() startReceiver() // 点击跳转到 FirstActivity findViewById<Button>(R.id.btn_first).setOnClickListener { val intent = Intent(this, FirstActivity::class.java) startActivity(intent) } // 点击跳转到 SecondActivity findViewById<Button>(R.id.btn_second).setOnClickListener { val intent = Intent(this, SecondActivity::class.java) startActivity(intent) } // 点击跳转到 ThirdActivity findViewById<Button>(R.id.btn_third).setOnClickListener { val intent = Intent(this, ThirdActivity::class.java) startActivity(intent) } } private fun startTimer() { lifecycleScope.launch { var count = 0 while (true) { MessageChannel.channel.send(count) Log.d("Tyhoo", "主页面 send: $count") count++ delay(1000) } } } private fun startReceiver() { lifecycleScope.launch { while (true) { val count = MessageChannel.channel.receive() Log.d("Tyhoo", "主页面 received: $count") } } } override fun onDestroy() { super.onDestroy() MessageChannel.channel.cancel() } }
注:在 MainActivity 的 startTimer 函数中,使用协程和循环不断地发送计数值到 MessageChannel.channel。这样做的效果是,每隔一秒钟,会将一个递增的计数值发送到通道中。
在 MainActivity 的 startReceiver 函数中,使用协程和循环不断地接收 MessageChannel.channel 中的计数值。这样做的效果是,一旦有计数值被发送到通道中,该函数就会立即接收并打印该计数值。
-
订阅者 FirstActivity(SecondActivity、ThirdActivity 同理)
class FirstActivity : AppCompatActivity() { private var job: Job? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) job = startReceivingCount() } private fun startReceivingCount(): Job { return lifecycleScope.launch { for (count in MessageChannel.channel) { Log.d("Tyhoo", "First 页面 received: $count") } } } override fun onDestroy() { super.onDestroy() job?.cancel() } }
注:在 FirstActivity 的 startReceivingCount 函数中,使用协程和循环不断地接收 MessageChannel.channel 中的计数值。这样做的效果是,在该页面中可以实时接收来自 MainActivity 发送的计数值,并打印在日志中。
三、总结
通过使用 Channel
,实现了一种简单的发布/订阅模式
,其中 MainActivity
充当了发布者,而 FirstActivity(SecondActivity、ThirdActivity )
充当了订阅者。计数值
通过 Channel
在两个页面之间进行传递
,并且能够实时更新
和响应
。这种方式提供了一种灵活的通信机制
,可以在不同的组件或模块之间进行数据传递
和事件通知
。