multithreading - Thread is freezing an activity on Android Application -


I am trying to use a thread on my application to delay the thread without any exhaustion activity. . The problem is that the thread on the activity thread is still freeze even when it is sleep (). What I want to do is a game like Simon says, I want to lighten a button for 2 seconds without deleting the activity, but it gets freeze. Thread is called a novo caller, and I start it at the end of the creative method. I'll paste the code, thanks in advance.

  package com.example.simondice; Import java.util.ArrayList; Import android App Import android.graphics.color; Importroid.os.Bundle; Import android.util.log; Import android.widget.Button; Public class broadens gigers activity {public button boton1Rojo; Public Button Button 2 Azul; Public button boton3veerde; Public button boton4 amarillo; Public Arrestist & lt; Integer & gt; ArrayJuego; // S. Guadra La Secucia del Jégigo Public Arrestist & lt; Integer & gt; ArrayJugador; // Start the Start for C Guard Lawrence @ Override Public Wide On Creation (Bundle Saved Instantstate) {Super. Contents (Saved Instantstate); SetContentView (R.layout.jugar); Boton1Rojo = (button) Find VibibIID (R.B. button 1); Boton2Azul = (button) findViewById (R.id.button2); Boton3Verde = (Button) Find ViewById (R.id.button3); Boton4Amarillo = (Button) findViewById (R.id.button4); // cambiumos l color de los boton botan 1 rozo .Setbackgram color (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)); // Botons a disabled al Inkier Botan 1 rozo.et enabled (wrong); Boton2Azul.setEnabled (false); Boton3Verde.setEnabled (wrong); Boton4Amarillo.setEnabled (wrong); // iniciamos el juego NuevoColor juego = new NuevoColor (); Juego.start (); } // Create and Nuova color Public Class NuevoColor thread extension {@ Override public Boyd to run (for) {Rnonutithred (new Rnnabl) {@ Override public Boyd run () {} // Two-o Auto-generated strip Arenjjejo = New ArrayList & lt; Integer & gt; (); Try {int rand = 0; Rand = (int) Math.floor (Math.random () * 4 + 1); If (rand == 1) {// iluminamos los botones y los ao ± adimos al array botan 1 rosso.Setbackground queller (color .rgb (255, 0, 0)); Arrayjua.ed (1); Thread Solid (2000); Botan 1 rojo .Setbackgram Color (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));} if (rand == 3) {Boughton 3 Wardksetbakground Kwolr (color. rgb (0, 255, 0)); Saranjeugo.ed (3); Thread Solid (2000); Bottom 3 verse.Setbackgram Coiler (Color.rgb (0, 127, 0)); and {Botan 4 Emarilla .Setbackgram Color (Color. RGB (255, 255, 0)); Saranjugo.ed (4); Thread Solid (2000); Bottom 4 amarillo .Setbackgram Color (Color. RGB (127, 127, 0)); }} Grip (Interrupted E) E. // TODO Automatic Holding Block e.printStackTrace (); }}}); }}   

}

}

Edit: Thanks to all for replies, now I know whats to run () After some changes the class is working, just when it is useful:

  the expansion of the public class NuevoColor thread {@Override Public Zero Run ({{{Thread. Sleep (1000) ;} catch (exception e) {// TODO auto-generated holding block e.printStackTrace ();} RunOnUiThread (new Rnnabl) {@Override public void run () {// TODO auto-generated method stub cambiarColar ();} }); Try {Thread.Sleep (2000); } Grip (Exception E) {// TODO Auto generated blocking block e.printStackTrace (); } RunOnUiThread (New Runnabel) {@Override Public Zero () {// TODO Auto generated method stub colorOurinal ();}}); }}    

simply "threads.leep (2000)"; Out of the runable, which you run on the UI thread. I think you want to sleep in the thread in any case. So something like this:

  expand to public class NuevoColor thread {@Override Public Zero () {Thread.sleep (2000); RunOnUiThread (New Runnabel) {@Override Public runs zero () {// ...}}); }}   

There are some things going on inside the run of NuevoColor, and are sleeping, because you want the background thread to be stopped and the full use interface Do not be.

Whatever you throw in order to run the runner, of course, it will run on the UI thread, so it will freeze the full user interface until it is go. So there is no heavy work, even sleep does not come.

In general, think about it: Whatever you want to do, do it in the background thread. Whatever UI updates, (like setBackgroundColor () etc.), run it on the UI thread. In my case, I had just indicated Thread.Sleep (2000).

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -