安卓handler

private val hdl=Handler(Looper.getMainLooper()) 不需反复构造
hdl.postDelay(f, 1000)后面是延迟的毫秒数

以每秒执行任务为例

private val f = object : Runnable {
    override fun run() {
        val nw = SystemClock.uptimeMillis()
        val nx = nw + (1000 - nw % 1000) ##时间延迟修复
        hdl.postAtTime(this, nx) ## 先post了在写内容,时间更精确
        ... ## 具体内容
    }
}

特别注意,在onPause的时候把handler停了, 否则计时很多bug

停的时候写hdl.removeCallbacksAndMessages(null)

posted @ 2020-02-14 22:46  _zwl  阅读(4)  评论(0)    收藏  举报