等待2秒执行
1.使用Handler
Handler().postDelayed({
mDialog.dismiss()
}, 2000)
2.使用Timer
Timer().schedule(2000){
mDialog.dismiss()
}
3.使用协程
MainScope().launch{
delay(2000)
mDialog.dismiss()
}
每隔2秒执行
1.Handler+Runnable
private val handler by lazy { Handler() }
private val runnable by lazy {
object : Runnable {
override fun run() {
i("11111")
handler.postDelayed(this, 2000)
}
}
}
//启动
handler.post(runnable)
//销毁
handler.removeCallbacks(runnable)
2.Timer和TimerTask
private val timer by lazy { Timer() }
private val task by lazy { object :TimerTask(){
override fun run() {
i("11111")
}
} }
//启动
timer.schedule(task, 2000, 2000)
//销毁
timer.cancel()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)