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
以便在更新时显示?