The main thread in this program is doing too much work for whatever reason. I don't know how else to use views on the main thread though. I've tried using AsyncTask but I have no idea as to how I'm supposed to use it.
Thread thread2 = new Thread(){
public void run(){
while(running){
runOnUiThread(new Runnable() {
@Override
public void run() {
try{
if(red){
buttons[ran].setImageResource(R.drawable.button_red);
buttons[ran].setTag("Red");
Thread.sleep(duration);
if(buttons[ran].getTag().equals("Red")){
buttons[ran].setTag("Black");
buttons[ran].setImageResource(R.drawable.button_null);
lifeLost();
red = false;
}
}else if(white){
buttons[ran].setImageResource(R.drawable.button_white);
buttons[ran].setTag("White");
Thread.sleep(duration);
if(buttons[ran].getTag().equals("White")){
buttons[ran].setTag("Black");
buttons[ran].setImageResource(R.drawable.button_null);
lifeLost();
white = false;
}
}
}catch(Exception e){
}
}
});
try {
sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread2.start();
This thread is doing whatever non UI related tasks I could give it. The code itself actually works on this thread. However, it only works on newer devices and older ones seem to pick up on the bug instead of ignoring it.
@Override
public void run() {
random = new Random();
random2 = new Random();
try{
Thread.sleep(1000);
while(running){
ran = random.nextInt(9);
type = random2.nextInt(3);
if(lives > 0){
if(type == 0 || type == 2){
red = true;
}else{
white = true;
}
if(lives == 0){
Thread.sleep(1000);
Intent overIntent = new Intent(this, OverActivity.class);
overIntent.putExtra("point", score);
startActivity(overIntent);
overridePendingTransition(0, 0);
}
if(score == 10){
duration -= 50;
}else if(score == 20){
duration -= 50;
}else if(score == 30){
duration -= 50;
}else if(score == 40){
duration -= 50;
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}