infinite loop, blocking android studio interface

0

I have a problem with an infinite loop that I generate in a handler, the idea of the infinite loop is to wait for a button to be pressed that modifies the value of p, but the theme is this when it enters the infinite loop it blocks the interface and it does not allow me to press any of the 4 buttons They told me that with a new thread before handler the problem would be solved but it was not, maybe I put it wrong if someone can give me a solution I would appreciate it.

public void iniciarTimer() {
c=0;
encender=true;
n = 0;
generar();
final android.os.Handler handler = new android.os.Handler();
new Thread(new Runnable() {
    public void run() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                numero = list.get(n);
                if (encender == true) {
                    if (finalizar == false) {
                        switch (numero) {
                            case 1:

                                boton1.setBackgroundColor(Color.RED);
                                boton3.setBackgroundResource(R.color.azul);
                                boton2.setBackgroundResource(R.color.verde);
                                boton4.setBackgroundResource(R.color.amarillo);
                                break;
                            case 2:
                                boton2.setBackgroundColor(Color.GREEN);
                                boton3.setBackgroundResource(R.color.azul);
                                boton1.setBackgroundResource(R.color.rojo);
                                boton4.setBackgroundResource(R.color.amarillo);
                                break;
                            case 3:
                                boton3.setBackgroundColor(Color.BLUE);
                                boton2.setBackgroundResource(R.color.verde);
                                boton1.setBackgroundResource(R.color.rojo);
                                boton4.setBackgroundResource(R.color.amarillo);
                                break;
                            case 4:
                                boton4.setBackgroundColor(Color.YELLOW);
                                boton3.setBackgroundResource(R.color.azul);
                                boton2.setBackgroundResource(R.color.verde);
                                boton1.setBackgroundResource(R.color.rojo);
                                break;
                        }
//condicion para que aumente n +1 despues de la primer pasada del handler ya que el primer color lo prende en la segunda pasada
                        if (c != 0) {
                            n++;
                        }
                        c++;
//limite de puntos en el juego
                        if (n > 10) {
                            finalizar = true;
                            encender = false;
                            fin();
                        }
//en nivel dificil que valla aumentando la velocidad
                        dificil = nivel;
                        if (dificil == 750) {
                            nivel = (nivel - 10);
                        }
// una vez que se recorre la lista que entre en el if para comparar
                        if (n == list.size()) {
// recorrer la lista
                            for (a = (0); a < (list.size()); a++) {
// esperar un valor de p al presionar un boton.
                                p = 0;
                                if (p == 0) {
                                    a--;
                                }
// comparar si el boton presionado coincide con el de la lista de no ser asi que finalize la activity
                                if (p != 0) {
                                    if (list.get(a) != p) {
                                        encender = false;
                                        finalizar = true;
                                        fin();
                                    }
                                }
                            }
// si todos los botones coiniciden inicia el timer de vuelta para generar otro numero y arrancar a recorrer de 0
                            encender = false;

                        }
                        if (encender == true) {
                            if (finalizar == false) {
                                handler.postDelayed(this, nivel);
                            }
                        } else {
//suma el punto si todos los botones coinciden y reinicia el timer para generar otro numero y arrancar a recorrer de 0
                            sumar();
                            apagar();
                            iniciarTimer();
                        }
                    } else {
                        apagar();
                    }
                }
            }
        }, 1000);

    }
}).start();
}
    
asked by Houth 03.07.2017 в 23:59
source

0 answers