Using AsyncTask brings me problems with Thread.sleep ()

1

I have a problem with the use of threads in android.

It's very simple, I have a table with buttons on the screen. I try that, randomly, one changes color and after half a second, return to the original color, so that at the moment another one is lit again.

The part that gives me problems is with the threads:

class colores extends AsyncTask<String,String,String>{

    private AppCompatActivity actividad;
    private int aleatorio;

    public colores(AppCompatActivity cx){
        this.actividad=cx;
    }

    public void generarAleatorio(){
        aleatorio = (int) (Math.random() * (28 - 0) + 0);
    }

    @Override
    protected String doInBackground(String... params) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    generarAleatorio();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            boton[aleatorio].setBackgroundColor(Color.YELLOW);
                        }
                    });
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            boton[aleatorio].setBackgroundColor(Color.WHITE);
                        }
                    });
                }
            }
        }).start();
        return null;
    }
}

The previous code would be inside another class ("button" is an array of Button and 28 is random because of the size of said array).

The problem I have is that I change color only to yellow, then for half a second and put another button in yellow, without replacing the last change in white. The result is that everyone ends up in yellow without going through the target.

Could it be the problem of Thread.sleep() in conjunction with the two runOnUiThread() ?

    
asked by Josedu 06.03.2017 в 16:12
source

1 answer

-1

I do not recommend that you launch a Thread within the DoInBackground of an Asynktask and within that Thread plus Threads.

  

It's very simple, I have a table with buttons on the screen. Tried   that, randomly, one changes color and after half a second, come back   to the original color, so that when another one is lit again.

For this, try to implement a solution similar to this:

Do not use your Asynktask for this and where you called your Asynktask, call the method to paint the yellow button directly.

public void paintYellow(Button button){
    //...
    button.setBackgroundColor(Color.YELLOW);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            paintWhite(Button button);
        }
    }, 500); // 500 ms = 0.5 segundos
}

private void paintWhite(Button button){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            button.setBackgroundColor(Color.WHITE);
        }
    });
}
    
answered by 06.03.2017 / 16:51
source