1.timer&timerTask
val timer:Timer?=null
/**60秒倒计时结束自动跳转首页*/
private fun countDownTimeSixty() {
var countDownTime = 60
val timer = Timer()
val tk: TimerTask = object : TimerTask() {
@SuppressLint("SetTextI18n")
override fun run() {
runOnUiThread {
if (countDownTime <= 0) {
//倒计时结束执行的操作
} else {
binding.countDownTv.text = countDownTime.toString() + "s"
}
Log.i(this.toString(), "countDownTime: $countDownTime")
countDownTime--
}
}
}
timer!!.schedule(tk, 0, 1000)
}
override fun onDestroy() {
super.onDestroy()
timer!!.cancel()
}
2. 使用job