Kotlin 协程Job 代替 Handler执行延时任务 带取消

Kotlin 复制代码
private val handler = Handler(Looper.getMainLooper())
var runnable = Runnable { dismissProgressDialog() }
......
handler.postDelayed(runnable, (10 * 1000).toLong())
......
//取消任务
handler.removeCallbacks(runnable)
Kotlin 复制代码
private var job: Job? = null
......
job = lifecycleScope.launch {
            delay(10*1000)
            dismissProgressDialog()
        }
......
//取消任务
job?.cancel()
相关推荐
SomeOtherTime1 小时前
Rust 面向对象介绍(AI回答)
开发语言·python·rust
松仔log1 小时前
Kotlin中级——callbackFlow
java·前端·kotlin
zh_xuan1 小时前
kotlin常见面试题
开发语言·kotlin·面试题
mmsx2 小时前
耳切法切带洞多边形:论文能跑通,真实数据为什么崩
android·kotlin
洞见前行2 小时前
你的 Activity 关了,为什么还活着?Android 内存泄漏从发现、定位到线上治理
android·性能优化·leakcanary
草莓熊Lotso3 小时前
【C++ 基于微服务的即时通讯系统】文件存储子服务实战:UUID生成、文件IO封装与全接口单元测试
开发语言·网络·数据库·c++·微服务·架构·单元测试
Kapaseker4 小时前
Kotlin 又要加语法糖了:这次轮到 if-else
android·kotlin
huaqianzkh4 小时前
WinForm C#:循环修改控件UI闪烁卡顿?SuspendLayout / ResumeLayout 与 BeginUpdate 优化
开发语言·ui·c#
恋猫de小郭6 小时前
Flutter 3.50 & Dart 3.14 发布,快来看看有什么更新吧
android·前端·flutter