【问题标题】:Progressbar not updating in thread android进度条未在线程 android 中更新
【发布时间】:2011-12-31 12:29:28
【问题描述】:

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

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();

我的 catlog 文件也没有更新。请帮忙....

【问题讨论】:

    标签: android progress-bar download


    【解决方案1】:

    您不能从后台线程更新 UI,请使用 AsyncTask,因为 Android 人称它为 painless threading

    Here is a nice Tutorial 使用 ProgressBar 使用 AsyncTask

    【讨论】:

      【解决方案2】:

      不需要AsyncTask,在你的线程中使用MyActivity.this.runOnUiThread()。会做与 AsyncTask 相同的工作,但它会减少你重新编码所有内容的时间。 :) 干杯

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-12
        • 1970-01-01
        • 2012-05-22
        • 2015-07-30
        • 1970-01-01
        • 1970-01-01
        • 2015-04-18
        相关资源
        最近更新 更多