How can I programmatically cause a delay in Android?

Michael Marcel

I tried to use Thread.sleep() but it didn't work. When I use it, the app stops responding.

I need to put some delays in my code like this:

public void inicioJogo(){
        for (int jogada = 1; jogada <= 50; jogada++) {
            for (int contador = 0; contador < jogada; contador++){
                // HERE - Wait 1 sec before go to next line.
                btPedra[sequencia[contador]].setBackgroundResource(imagensHover[sequencia[contador]]);
                // HERE - Wait 1 sec before go to next line.
                btPedra[sequencia[contador]].setBackgroundResource(imagensNormal[sequencia[contador]]);
                // Now continue looping.
            }
        }
}

I tried to use Handler, like this:

private Handler handler = new Handler();
    for (int jogada = 1; jogada <= 50; jogada++) {
        for (int contador = 0; contador < jogada; contador++){
            handler.postDelayed(new Runnable () {
                @Override
                public void run() {
                    btPedra[sequencia[contador]].setBackgroundResource(imagensHover[sequencia[contador]]);
                }
            }, 1000);
        }
    }

But when I use it, the looping continue before waiting 1 sec. I need a delay that can stop the looping for 1 sec, go to the next line, and after that continue looping.

Simas

The reason why it doesn't respond is because you call sleep on the current thread. And the current thread you're dealing with is the UI thread. So basically you try to change the background and then sleep the thread preventing it to actually be changed until the sleeps are done.

You need to run your loop in another thread and sleep there so it doesn't affect the main thread:

final Handler handler = new Handler();
new Thread(new Runnable() {
    @Override
    public void run() {
        for (int jogada = 1; jogada <= 50; jogada++) {
            for (int contador = 0; contador < jogada; contador++){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        btPedra[sequencia[contador]].setBackgroundResource(imagensHover[sequencia[contador]]);
                    }
                });
            }
        }
    }
}).start();

Note that the background change is done via the handler you create before starting the new thread. That handler is created on the current thread, which is the UI thread, and every message posted to it will run on that thread.

So basically you loop and wait on another thread but do the background changing on the UI thread.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I add focus programmatically in Android?

How can I programmatically include layout in Android?

How can I lock an icon in Android (programmatically)?

How Can I Do Android Numpad Programmatically

How can I delay splash launch screen programmatically in Swift Xcode iOS

How can I delay submit

Can I cause Xcode's debugger to break programmatically?

How can i display programmatically a textview next to a button in a RelativeLayout in Android?

How can I programmatically decompile dex files within Android?

Android: How can I programmatically lock the brightness slider?

How can I determine the latest Android version programmatically?

How can I convert an XML to make a programmatically view in Android Java

How can I discover CPU usage of my application in Android (programmatically)?

How can I turn ON/OFF "Show notification" programmatically in android

How can I enable or disable the GPS programmatically on Android?

How can I close/hide the Android soft keyboard programmatically?

How can I programmatically force stop an Android app with Java?

How can I configure Launcher activity programmatically in android?

How can i get current playing song in Android programmatically

How can I know programmatically if a notification channel is enabled on Android O?

How can I get android View kind programmatically?

How can I detect programmatically if the Android Device is in Dark Mode?

Android Kotlin - How can I programmatically disconnect a bluetooth device?

How can I find android apk installation activity programmatically?

How can I programmatically store a birthday with no year in Android?

How can I delay an observable only if it returns faster than the delay

How can I create an observable with a delay

How can i delay multiple if conditional statements?

How can I emit values with delay in RXJS?