美文网首页
解决postDelayed崩溃问题,postDelayed &&

解决postDelayed崩溃问题,postDelayed &&

作者: 海蓝精 | 来源:发表于2017-07-19 13:08 被阅读0次

# postDelayed && postAtTime #

1.postDelayed(Runnable r, long delayMillis)方法是延迟执行r接口,delayMilles是延迟的时间,即使在Activity销毁之后还会继续执行。

2.postAtTime(Runnable r, Object token, long uptimeMillis)方法是定时执行r接口。第二个参数是标记值,第三个参数是定时时间,计算方法是从手机开机开始计算,之后的毫秒值执行接口。比如从开机到现在是10000毫秒,你想在3000毫秒后执行r,就填入10000 + 3000。当前开机时间获得方法是SystemClock.uptimeMillis()。

3.可能会遇到问题就是,使用postDelayed时候r中含有Context的情况,那么在Activity销毁后还会继续执行,就会造成崩溃,但是又没法取消。解决办法就是使用postAtTime,第二个参数填入this,在onDestory中添加handler.removeCallbacksAndMessages(this),就会取消所有的Runnable接口,就不会继续执行。

相关文章

网友评论

      本文标题:解决postDelayed崩溃问题,postDelayed &&

      本文链接:https://www.haomeiwen.com/subject/oarlkxtx.html