How to delay execution android How to delay execution android multithreading multithreading

How to delay execution android


Using a Handler, which is a good idea if you are executing from a UI thread...

    final Handler h = new Handler();    final Runnable r2 = new Runnable() {        @Override        public void run() {            // do second thing        }    };    Runnable r1 = new Runnable() {        @Override        public void run() {            // do first thing            h.postDelayed(r2, 10000); // 10 second delay        }    };    h.postDelayed(r1, 5000); // 5 second delay


Just to add a sample : The following code can be executed outside of the UI thread.Definitely, Handler must be use to delay task in Android

Handler handler = new Handler(Looper.getMainLooper());final Runnable r = new Runnable() {    public void run() {        //do your stuff here after DELAY milliseconds    }};handler.postDelayed(r, DELAY);