当我下载文件时,我希望进度条与此同步。但不是,我的代码是:
Thread thread = new Thread() {
public void run() {
try {
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
//add up the size so we know how much is downloaded
downloadedSize += bufferLength;
pbar.setProgress((downloadedSize/totalSize)*100);
pbar.refreshDrawableState();
Log.d("CURRENT:", "" + (downloadedSize/totalSize)*100);
}
} catch (Exception ex) {
}
}
};
thread.start();
我的目录文件也没有更新。请提供任何帮助....
最佳答案
您不能从后台线程更新 UI,请使用 AsyncTask
因为 Android 人称它为 painless threading
Here is a nice Tutorial使用 ProgressBar
使用 AsyncTask
关于android - 进度条未在线程 android 中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8688084/