我對此很陌生,所以也許這對每個人都是微不足道的,但我無法弄清楚,爲什麼這不起作用。我已經讀過它,嘗試了很多方法,但仍然無法工作。所以我想在android(java)中暫停一個線程。我想讓它運行,凍結屏幕1秒,然後繼續工作。就這樣。爲什麼這不起作用?Android/Java - 暫停線程
public class Game extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
runner.join();
runner.start();
// do stuff
pause();
// do stuff
}
private boolean running = true;
private boolean paused = false;
public void setRunning(boolean run) {
running = run;
}
public void pause() {
paused = true;
}
Thread runner = new Thread() {
public void run() {
while (running) {
try {
//do stuff
Thread.sleep(100);
while (paused) {
try {
Thread.sleep(1000);
} catch (Exception e) {
} finally {
paused = false;
}
}
} catch (Exception e) {
}
}
}
};
}
你在while(暫停)循環中的異常可能會觸發。將異常消息記錄到catch塊中的控制檯。 –