android - 进度条未在线程 android 中更新

标签 android progress-bar download

当我下载文件时,我希望进度条与此同步。但不是,我的代码是:

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/

相关文章:

Android 错误 - close() 从未在数据库上显式调用

html - HTML+CSS 中的不确定进度条

javascript - 单击 anchor ,在同一窗口中下载文档

java - 如何使用进度条为 TextView 制作动画

php - 使用服务器发送事件在 cakephp 3 中实现数据库插入进度条

ios - 获取大小文件NSURLConnection?

java - 下载 OSM Tiles,连接被对等方重置

android - 音板应用程序上的媒体播放器错误

android - 检索到的数据不是按升序排列的?

android - 首选项未使用 xml 中的默认值进行初始化