Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android equivalent of setTimeout and clearTimeout of javascript?

Tags:

android

timer

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?

like image 635
eugene Avatar asked Mar 15 '23 04:03

eugene


1 Answers

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);
like image 77
antonio Avatar answered Apr 24 '23 21:04

antonio