返回活动后,ProgressBar未正确更新

时间:2011-09-30 03:21:05

标签: android android-progressbar

MyBookshelf扩展Activity并实现异步下载和解压缩进程调用的接口。

异步下载过程定期从接口调用onBookDownloadPercent(float)。我的活动定义包括:

public class MyBookshelf extends Activity implements BookDownloadListener {

    private ProgressBar mProgress;

    // .......
    // .......

    @Override
    public void onBookDownloadPercent(float _percent_finished) {
        final float final_percent  = _percent_finished;
        runOnUiThread(new Runnable() {
            public void run()
            {
                DebugLog.d(TAG, "onBookDownloadPercent " + Float.toString(final_percent));
                mProgress.setProgress((int) final_percent);
            }
        });
    }
}

在LogCat中,我的函数DebugLog.d向我保证UI线程正在调用mProgress.setProgress()

09-30 11:37:06.698: DEBUG/MyBookshelf(2069): onBookDownloadPercent 0.0014221431; thread 1
09-30 11:37:07.288: DEBUG/MyBookshelf(2069): onBookDownloadPercent 1.0004596; thread 1
09-30 11:37:07.838: DEBUG/MyBookshelf(2069): onBookDownloadPercent 2.0001705; thread 1
09-30 11:37:08.318: DEBUG/MyBookshelf(2069): onBookDownloadPercent 3.0014038; thread 1
09-30 11:37:09.098: DEBUG/MyBookshelf(2069): onBookDownloadPercent 4.0002813; thread 1
09-30 11:37:09.468: DEBUG/MyBookshelf(2069): onBookDownloadPercent 5.0003366; thread 1
09-30 11:37:09.818: DEBUG/MyBookshelf(2069): onBookDownloadPercent 6.0003924; thread 1
09-30 11:37:10.158: DEBUG/MyBookshelf(2069): onBookDownloadPercent 7.0004926; thread 1
09-30 11:37:10.588: DEBUG/MyBookshelf(2069): onBookDownloadPercent 8.000748; thread 1
09-30 11:37:10.938: DEBUG/MyBookshelf(2069): onBookDownloadPercent 9.000215; thread 1
09-30 11:37:11.368: DEBUG/MyBookshelf(2069): onBookDownloadPercent 10.00017; thread 1

我有一个类似的异步解压缩任务接口:

@Override
public void onBookUnzipPercent(float _percent_finished) {
    final float final_percent  = _percent_finished;
    runOnUiThread(new Runnable() {
        public void run()
        {
            DebugLog.d(TAG, "onBookUnzipPercent " + Float.toString(final_percent));
            mProgress.setProgress((int) final_percent);
        }
    });
}

首先,MyBookshelf调用异步任务,下载第一本书,进度条正确更新(0到100%)。

其次,MyBookshelf调用异步任务,解压缩第一本书,并且进度条正确更新(0到100%)。

第三,MyBookshelf致电startActivity(nextIntent)来阅读本书并自行调用finish()

第四,我读完书后回到MyBookshelf:

nextIntent = new Intent(getBaseContext(), MyBookshelf.class);

下载下一本书时出现问题:发生下载,LogCat会记录更新,但进度条仅显示为空白灰色,并且在下载和解压缩时更新下一本书。我已经使用LogCat确认线程1确实通过onBookDownloadPerecent Runnable

运行

回到mProgress时,我是否需要对MyBookshelf做一些有趣的事情,以便在更新时显示?

2 个答案:

答案 0 :(得分:1)

我的解决方案是让mProgressBar静态:

public class MyBookshelf extends Activity implements BookDownloadListener {

    static private ProgressBar mProgress;

    // .......
    // .......

    @Override
    public void onBookDownloadPercent(float _percent_finished) {
        final float final_percent  = _percent_finished;
        runOnUiThread(new Runnable() {
            public void run()
            {
                DebugLog.d(TAG, "onBookDownloadPercent " + Float.toString(final_percent));
                mProgress.setProgress((int) final_percent);
            }
        });
    }
}

答案 1 :(得分:0)

为什么在读完这本书后回到myBookShelf时你不使用finish()?

似乎一切正常,但我对这行代码有点怀疑

nextIntent = new Intent(getBaseContext(), MyBookshelf.class);