android 关于使用协程的一些问题

问题1:能不能在GlobalScope.launch(Dispatchers.Main){} 做耗时事情

问题2: delay() 算不算耗时的

案例1:

复制代码
        findViewById<View>(R.id.btn1).setOnClickListener {
            GlobalScope.launch(Dispatchers.Main){
                 Thread.sleep(21000)
                 Toast.makeText(this@MainActivity,"sdfs",Toast.LENGTH_SHORT).show()
            }
        }

效果:直接ANR了

案例2:

复制代码
      findViewById<View>(R.id.btn1).setOnClickListener { 

            GlobalScope.launch(Dispatchers.Main){
                 delay(20000)
                Toast.makeText(this@MainActivity,"sdfs",Toast.LENGTH_SHORT).show()
            }
        }

效果:20秒以后显示了 Toast

相关推荐
一航jason10 小时前
Android平台推理框架及试用场景模型对比
android·人工智能·ai·架构·ai编程·llama
一航jason10 小时前
Android 端侧大模型推理框架对比
android·人工智能·ai·ai编程·llama·ai-native
新时代牛马10 小时前
cyclictest 毛刺从哪来?从ftrace、latencytop、perf到IRQ/调度延迟定位
android·开发语言·python·kotlin
光电的一只菜鸡12 小时前
为什么调整LSC会对AF产生影响
android·开发语言·kotlin
凛_Lin~~12 小时前
LiveData 源码解析
android·安卓·livedata
hai_android13 小时前
FusibleFlow:Kotlin Flow 的融合机制原理
android·kotlin
龚寿生13 小时前
23-敏捷开发实战:从Scrum到看板的团队协作方法论
android·学习·scrum·敏捷流程
zhangguojia714 小时前
Activity启动流程(四):从setContentView到View树创建与Window挂载
android
三少爷的鞋17 小时前
Kotlin 2026:裁员、AI、Rust——黄金时代结束了吗?Jake Wharton 为你解答
android