How can I wait for 10 second without locking application UI in android [duplicate] How can I wait for 10 second without locking application UI in android [duplicate] java java

How can I wait for 10 second without locking application UI in android [duplicate]


You can use this:

Handler handler = new Handler();handler.postDelayed(new Runnable() {    public void run() {     // Actions to do after 10 seconds    }}, 10000);

For Stop the Handler, You can try this:handler.removeCallbacksAndMessages(null);


You never want to call thread.sleep() on the UI thread as it sounds like you have figured out. This freezes the UI and is always a bad thing to do. You can use a separate Thread and postDelayed

This SO answer shows how to do that as well as several other options

Handler

TimerTask

You can look at these and see which will work best for your particular situation


1with handler:

handler.sendEmptyMessageDelayed(1, 10000);}private Handler handler = new Handler() {    @Override    public void handleMessage(Message msg) {        if (msg.what == 1) {           //your code        }    }};