2

How can I stop the counter of the Handler when it shouldn't count anymore? Maybe you can tell me how to do with the code below.

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

                viewFlipper.setDisplayedChild(8);
        }
    }, 20000);
}
silvia_aut
  • 1,481
  • 6
  • 19
  • 33

1 Answers1

8

Use 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.
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • yes thanks, but what is nhandlerTask? Should be the runnable, right? But what is it in my case? – silvia_aut Sep 24 '13 at 06:06
  • I didn't wrote more than I posted. Thats my Handler and it works, but I need to stop it and I don't know what I do with my Runnable. – silvia_aut Sep 24 '13 at 06:08
  • you can also pass `null` and also check this http://stackoverflow.com/questions/17839419/android-thread-for-a-timer if it helps. If token is null, all callbacks will be removed. – Raghunandan Sep 24 '13 at 06:09
  • @silvia_aut you could pass `this` or `null` yes it should be `runnable` – Raghunandan Sep 24 '13 at 06:17