Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin 使用 Channel 实现发布/订阅模式 #4

Open
cnwutianhao opened this issue Jan 5, 2024 · 0 comments
Open

Kotlin 使用 Channel 实现发布/订阅模式 #4

cnwutianhao opened this issue Jan 5, 2024 · 0 comments
Labels

Comments

@cnwutianhao
Copy link
Owner

一、角色设定

  1. 发布者:MainActivity
  2. 订阅者:FirstActivity、SecondActivity、ThirdActivity

二、代码实现

  1. 定义全局 Channel

     object MessageChannel {
         val channel = Channel<Int>()
     }

    注:MessageChannel.channel 是一个 Channel 对象,用于在不同页面之间传递整数类型的计数值。它充当了一个中介,允许一个页面发送计数值,而其他页面则可以接收这些计数值。

  2. 发布者 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 中的计数值。这样做的效果是,一旦有计数值被发送到通道中,该函数就会立即接收并打印该计数值。

  3. 订阅者 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 在两个页面之间进行传递,并且能够实时更新响应。这种方式提供了一种灵活的通信机制,可以在不同的组件或模块之间进行数据传递事件通知

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant