如何在postDelayed模式下停止处理程序?

时间:2013-09-24 06:03:24

标签: android handler runnable

如果处理程序的计数器不再计数,我怎么能停止它? 也许你可以告诉我如何处理下面的代码。

 public void handler() {
    nHandler.postDelayed(new Runnable() {
        @Override
        public void run() {

                viewFlipper.setDisplayedChild(8);
        }
    }, 20000);
}

1 个答案:

答案 0 :(得分:8)

使用removeCallbacks

nHandler.removeCallbacks(nhandlerTask);

http://developer.android.com/reference/android/os/Handler.html#removeCallbacks(java.lang.Runnable)

public final void removeCallbacks (Runnable r)

Added in API level 1
Remove any pending posts of Runnable r that are in the message queue.