5

There's an answer for setTimeout https://stackoverflow.com/a/18381353/433570

It doesn't provide info whether we can cancel the timer as we could in javascript.

Is there a timer that is cancellable in android?

Community
  • 1
  • 1
eugene
  • 39,839
  • 68
  • 255
  • 489

2 Answers2

8

According to the documentation of Handler

public final void removeCallbacks (Runnable r)

Added in API level 1

Remove any pending posts of Runnable r that are in the message queue.

Example code:

Runnable runnable = new Runnable() {
    public void run() {
        Log.i("Tag", "Runnable running!");
    }
};

Handler handler = new android.os.Handler();
handler.postDelayed(runnable, 1000);

handler.removeCallbacks(runnable);
antonio
  • 18,044
  • 4
  • 45
  • 61
0

For instance, you may use removeMessages, removeCallbacks methods

ivan
  • 359
  • 3
  • 11