0
我有一個生成迷宮的應用程序。在迷宮一代中,我想更新進度條。但是,我看不到一個好方法來做到這一點。進度條在一個線程上運行,而迷宮生成則在另一個線程上運行。我怎麼讓這兩個來回傳遞一個簡單的int?對不起,如果這是一個不好的問題,我是一個初學者。Android - 使用另一個線程的值更新進度條
protected void incrementProgress() throws InterruptedException {
generateMaze(); //Generates the maze by launching a new thread
while(progressBarStatus < 100) {
progressBarStatus = maze.getIntPercentDone(); //This does not get updated
Thread.sleep(10);
progressBarHandler.post(new Runnable() {
@Override
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
if (progressBarStatus >= 100) { //This condition does not get met
Thread.sleep(100);
progressBar.dismiss();
}
}
/**
* Dummy maze generation method that has a counter to simulate progress
* @return
*/
protected void generateMaze() { //Method that builds a maze
mazeBuilder.build(maze, Constants.SKILL_X[0], Constants.SKILL_Y[0], Constants.SKILL_ROOMS[0], Constants.SKILL_PARTCT[0]);
try {
mazeBuilder.buildThread.join();
}
catch (InterruptedException e) {
mazeBuilder.buildThread.interrupt();
}
}
我已經看過一些例子,但是我不明白我們如何從mazebuilder將publishProgress()發佈到活動中? – mrQWERTY 2014-11-21 07:52:00
已更新的回答。你也可以閱讀http://www.codeproject.com/Articles/580172/Asynchronous-Tasks-using-AsyncTask-另一個例子 – Arnab 2014-11-21 08:05:20