我在更新进度条时遇到问题。我正在单独的线程中更新进度条,并且进度条进度所依赖的变量(这是一个类变量)在另一个线程中更新。因此,进度对话框显示但总是 0% 不更新它的进度。请帮帮我。
public void setProgressbar()
{
progressBar = new ProgressDialog(context);
progressBar.setCancelable(true);
progressBar.setMessage("File downloading ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
Thread thread = new Thread()
{
public void run()
{
while(progressBar.getProgress() < 100)
{
Log.v("progressbar", getProgress()+"");
progressBar.setProgress(getProgress());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
thread.start();
更新值代码
Thread thread = new Thread()
{
public void run()
{
.
.
.
while((bytesRead = is.read(bytearray, 0, bytearray.length)) != -1)
{
bos.write(bytearray, 0, bytesRead);
totaldownload = totaldownload + bytesRead;
Log.v("downloadign ", totaldownload+"");
// progressBar.setProgress((int) ((totaldownload/sizeoffile) * 100));
}
bos.close();
.
.
.
};
thread.start()
和 getPrgoress 方法
public int getProgress()
{
return (int) ((totaldownload/sizeoffile) * 100);
}