Handler handler = new Handler(); long delay = 1000; // 1 second delay Runnable task = new Runnable() { @Override public void run() { // code to be executed after 1 second } }; handler.postAtTime(task, SystemClock.uptimeMillis() + delay);
Handler handler = new Handler(); long delay = 5000; // 5 second delay Bundle data = new Bundle(); data.putString("message", "Hello World!"); Message msg = handler.obtainMessage(); msg.setData(data); handler.postAtTime(new Runnable() { @Override public void run() { // code to handle the message with the data } }, SystemClock.uptimeMillis() + delay);In this example, a Handler object is created, and a Message object is obtained from the handler. The message contains a Bundle with a message "Hello World!". The postAtTime method is then called with a Runnable object that will execute the code to handle the message after a delay of 5 seconds. Package library: android.os.