线程正在冻结Android应用程序上的活动



我正在尝试在我的应用程序上使用线程来延迟该线程而不冻结活动。问题是即使我在线程上有 Thread.sleep(),活动也会冻结。我想做的是像西蒙说的那样,我想突出显示一个按钮 2 秒钟而不冻结活动,但它被冻结了。该线程称为NuevoColor,我在onCreate方法的末尾启动它。我将在此处粘贴代码,提前感谢您。

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();
                }
            }
        });
    }
}

}

编辑:感谢大家的回答,现在我更清楚runOnUiThread()的用途。这是经过一些更改后工作的类,以防万一有人发现它很有用:

    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();
            }
        });
    }
}

只需将"Thread.sleep(2000);"移动到在UI线程上运行的Runnable之外。我看到你无论如何都想睡线程。所以像这样:

public class NuevoColor extends Thread {
    @Override public void run() {
        Thread.sleep(2000);
        runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // ...
                }
            });
    }
}

你在NuevoColor的run()中运行的任何内容都是后台工作,睡眠应该在那里,因为你希望暂停后台线程,而不是整个使用界面。

当然,您扔到 runOnUiThread() 的 Runnable 中的任何内容都将在 UI 线程上运行,因此它会冻结整个用户界面,直到完成。所以那里没有繁重的工作,也没有睡觉。

一般来说,可以

这样想:任何你想做的事情,都在后台线程中做。任何更新UI的东西(如setBackgroundColor()等),都在UI线程上运行它。在您的情况下,只需按照我的指示移动 Thread.sleep(2000)。

由于您在runOnUiThread中调用Thread.sleep(2000),因此UI冻结如ρяσѕρуяя所指出的那样。不要在 ui 线程上调用睡眠。不应阻止 UI 线程。

参考这个 Thread.sleep() 是否使 UI 线程进入睡眠状态?

问题的解决方法:调用异步任务并在异步任务的 doinbackground() 内部调用异步任务的 Thread.sleep(2000) .doinbackground() 不在 UI 线程上运行。这是如何使用异步任务异步任务安卓示例

最新更新