我对android中的进度条有疑问。我还没有实施它,但由于时间不足,我想确保何时实施我最好清楚将要发生什么以及为什么..
dialog = new ProgressDialog(this);
dialog.setCancelable(true);
dialog.setMessage("Loading...");
// set the progress to be horizontal
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// reset the bar to the default value of 0
dialog.setProgress(0);
// get the maximum value
EditText max = (EditText) findViewById(R.id.maximum);
// convert the text value to a integer
int maximum = Integer.parseInt(max.getText().toString());
// set the maximum value
dialog.setMax(maximum);
// display the progressbar
dialog.show();
// create a thread for updating the progress bar
Thread background = new Thread (new Runnable() {
public void run() {
try {
// enter the code to be run while displaying the progressbar.
//
// This example is just going to increment the progress bar:
// So keep running until the progress value reaches maximum value
while (dialog.getProgress()<= dialog.getMax()) {
// wait 500ms between each update
Thread.sleep(500);
// active the update handler
progressHandler.sendMessage(progressHandler.obtainMessage());
}
} catch (java.lang.InterruptedException e) {
// if something fails do something smart
}
}
});
// start the background thread
background.start();
}
// handler for the background updating
Handler progressHandler = new Handler() {
public void handleMessage(Message msg) {
dialog.incrementProgressBy(increment);
}
};
这是我从某处获取的上述代码......正如这段代码所说,我必须将我的实际代码保持在下面尝试以保持进度条运行。我怀疑我的代码是一个非常冗长的代码,其中包含嵌套的 for 循环(用所有这些位和字节解析整个文件)..如果我在该部分的那个部分保留我的冗长过程,它如何更新进度条直到它完成任务?我错过了一些概念吗?请解释我如何保持我的进程运行并更新进度条?