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