我有两个线程t1和t2。每个都有代码
getWindow().getDecorView().setBackgroundColor(Color.GREEN);
问题是该行不适用于Thread t2和主线程。
以下是代码:
package arj.developer.jaadu;
import android.app.Activity;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
MediaPlayer mp1,mp2;
View v;
Thread t1 = new Thread(){
public void run(){
getWindow().getDecorView().setBackgroundColor(Color.CYAN);
mp1=MediaPlayer.create(MainActivity.this,R.raw.jaad);
mp1.start();
}
};
Thread t2= new Thread(){
public void run(){
getWindow().getDecorView().setBackgroundColor(Color.MAGENTA);
mp2=MediaPlayer.create(MainActivity.this,R.raw.jaad);
mp1.stop();
mp2.start();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().getDecorView().setBackgroundColor(Color.GREEN);
try {
t1.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t1.start();
try {
t2.sleep(30000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t2.start();
}
}
答案 0 :(得分:0)
sleep()导致CURRENT线程进入休眠状态,因此t1.sleep(10000)
和t2.sleep(10000)
都导致主线程进入休眠状态!
将睡眠方法移至t1& t2的run()
方法,您可能也应该在主线程中运行getWindow().getDecorView().setBackgroundColor(_color_);
。
package arj.developer.jaadu;
import android.app.Activity;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
MediaPlayer mp1,mp2;
View v;
Thread t1 = new Thread(){
public void run(){
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
getWindow().getDecorView().setBackgroundColor(Color.CYAN);
}
});
mp1=MediaPlayer.create(MainActivity.this,R.raw.jaad);
mp1.start();
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
getWindow().getDecorView().setBackgroundColor(Color.MAGENTA);
}
});
mp2=MediaPlayer.create(MainActivity.this,R.raw.jaad);
mp1.stop();
mp2.start();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().getDecorView().setBackgroundColor(Color.GREEN);
t1.start();
}
}