# postDelayed && postAtTime #
1.postDelayed(Runnable r, long delayMillis)方法是延遲執(zhí)行r接口,delayMilles是延遲的時間,即使在Activity銷毀之后還會繼續(xù)執(zhí)行。
2.postAtTime(Runnable r, Object token, long uptimeMillis)方法是定時執(zhí)行r接口。第二個參數(shù)是標(biāo)記值,第三個參數(shù)是定時時間,計算方法是從手機開機開始計算,之后的毫秒值執(zhí)行接口。比如從開機到現(xiàn)在是10000毫秒,你想在3000毫秒后執(zhí)行r,就填入10000 + 3000。當(dāng)前開機時間獲得方法是SystemClock.uptimeMillis()。
3.可能會遇到問題就是,使用postDelayed時候r中含有Context的情況,那么在Activity銷毀后還會繼續(xù)執(zhí)行,就會造成崩潰,但是又沒法取消。解決辦法就是使用postAtTime,第二個參數(shù)填入this,在onDestory中添加handler.removeCallbacksAndMessages(this),就會取消所有的Runnable接口,就不會繼續(xù)執(zhí)行。