Introduction

postDelayed() causes the runnable to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which this handler is attached. This API can be accessed from

  • Handler.postDelayed(Runnable r, long delayMillis)
  • View.postDelayed(Runnable action, long delayMillis)

From the source, View.postDelayed() is using Handler.postDelayed() on an internal handler.

Handler PostDelayed

postDelayed(Runnable, time) method is used to send a message with a delayed time. gameOver() method will be invoked once the handler is done waiting 1000 milliseconds.

final Runnable r = new Runnable() {
  public void run() {
    gameOver();
  }
};

handler.postDelayed(r, 1000);

 

View PostDelayed

Similarly postDelayed(Runnable, time) method can be used with as shown below.

public class MyActiviy extends Activity {
  // Other code

  // postDelayed example
  private void foo(View v) {
    v.postDelayed(new Runnable() {
        public void run() {
            // some delayed work
        }
    }, 1000);
  }
}