Skip to content Skip to sidebar Skip to footer

Android: How To Set Time Gap In Methods, When Sleep Or Wait Is Not Working

I built a TicTacToe game for android. And i want that there should be some time gap or time difference between User's turn and Computer's turn. Mean i want like a sleep() or any ot

Solution 1:

If you want to delay some code, you should post it in a handler with a delay time. This way the handler will wait the given time and then execute the given runnable.

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            cpuPlay();
        }
    }, TIME_TO_WAIT_IN_MS);

Solution 2:

If you do a Thread.sleep in the UI Thread, you sleep the main thread, so you need to do your sleep in a thread or use a handler or an AsyncTask.

Post a Comment for "Android: How To Set Time Gap In Methods, When Sleep Or Wait Is Not Working"