I'm trying to use a thread on my application to delay that thread without freezing the activity. The problem is that the activity is getting freezed even if i have the Thread.sleep() on the thread. What i want to do is a game like Simon says, i want to hightlight a button for 2 seconds without freezing the activity, but it gets freezed. The thread is called NuevoColor, and i start it at the end of the onCreate method. I'll paste the code here, thanks in advance.
package com.example.simondice;
import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
public class Jugar extends Activity {
public Button boton1Rojo;
public Button boton2Azul;
public Button boton3Verde;
public Button boton4Amarillo;
public ArrayList<Integer> arrayJuego; //se guarda la secuencia del juego
public ArrayList<Integer> arrayJugador; //se guarda la secuencia introducida por el jugador
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jugar);
boton1Rojo = (Button) findViewById(R.id.button1);
boton2Azul = (Button) findViewById(R.id.button2);
boton3Verde = (Button) findViewById(R.id.button3);
boton4Amarillo = (Button) findViewById(R.id.button4);
//cambiamos el color de los botones
boton1Rojo.setBackgroundColor(Color.rgb(127, 0, 0));
boton2Azul.setBackgroundColor(Color.rgb(0, 0, 127));
boton3Verde.setBackgroundColor(Color.rgb(0, 127, 0));
boton4Amarillo.setBackgroundColor(Color.rgb(127, 127, 0));
//botones a disable al iniciar
boton1Rojo.setEnabled(false);
boton2Azul.setEnabled(false);
boton3Verde.setEnabled(false);
boton4Amarillo.setEnabled(false);
//iniciamos el juego
NuevoColor juego = new NuevoColor();
juego.start();
}
// Crea un nuevo color para el juego
public class NuevoColor extends Thread {
@Override public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
arrayJuego = new ArrayList<Integer>();
try {
int rand = 0;
rand = (int) Math.floor(Math.random()*4+1);
if(rand==1) { //iluminamos los botones y los añadimos al array
boton1Rojo.setBackgroundColor(Color.rgb(255, 0, 0));
arrayJuego.add(1);
Thread.sleep(2000);
boton1Rojo.setBackgroundColor(Color.rgb(127, 0, 0));
} else if(rand==2) {
boton2Azul.setBackgroundColor(Color.rgb(0, 0, 255));
arrayJuego.add(2);
Thread.sleep(2000);
boton2Azul.setBackgroundColor(Color.rgb(0, 0, 127));
} else if(rand==3) {
boton3Verde.setBackgroundColor(Color.rgb(0, 255, 0));
arrayJuego.add(3);
Thread.sleep(2000);
boton3Verde.setBackgroundColor(Color.rgb(0, 127, 0));
} else {
boton4Amarillo.setBackgroundColor(Color.rgb(255, 255, 0));
arrayJuego.add(4);
Thread.sleep(2000);
boton4Amarillo.setBackgroundColor(Color.rgb(127, 127, 0));
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
}
Edit: thanks everyone for the answers, now i know better whats runOnUiThread for() . Here's the class working after some changes, just in case someone finds it usefull:
public class NuevoColor extends Thread {
@Override public void run() {
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
cambiarColor();
}
});
try {
Thread.sleep(2000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
colorOriginal();
}
});
}
}