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

相关推荐
say_fall11 分钟前
Linux系统编程(十一):深入理解Linux进程地址空间
android·linux·运维
程思扬16 分钟前
Android 大厂编码规范
android·网络·安全·开源·流程图
SoftLipaRZC22 分钟前
C语言文件:文件操作完全指南
android·java·c语言
2501_9160074732 分钟前
不用 Mac 也可以 Windows下管理iOS描述文件的非Xcode完整指南
android·ios·小程序·https·uni-app·iphone·webview
杉氧15 小时前
100% Kotlin:基于 KMP + Compose Multiplatform 的全栈架构实战(Clean Architecture + MVI)
android·架构
小仙女喂得猪15 小时前
AI 写 Android 代码老翻车?我把移动端的 Harness 系统开源了
android·github·ai编程
杉氧15 小时前
第一篇:从一个 Dagger 报错开始:手把手带你搭建 Hilt 依赖注入的护城河
android·架构
咋吃都不胖lyh15 小时前
短期记忆和长期记忆都存 MySQL
android·java·开发语言
杊页17 小时前
系列三:组件化与模块化进阶 | 第8篇 组件化与模块化核心实战区别:大型项目架构的必由之路
android·android jetpack