求解:AsyncTask同步更新UI的原理

tiantangniao232 tiantangniao232 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
从网络上下载一个文件,在AsyncTask的doInBackground里实现下载并且把进度通过publishProgress传给UI线程更新UI上的进度条,想问一下AsyncTask如何保证doInBackground里的publishProgress发布的进度和UI线程更新的进度同步的??
因为下载和更新是在两个线程进行的,AsyncTask如何保证更新同步的:
Thread is running 10 ----doInBackground发布进度
handleMessage ----UI更新进度
Thread is running 20 ----依次进行
handleMessage
Thread is running 30
handleMessage
Thread is running 40
handleMessage
Thread is running 50
handleMessage

而不是像下面这样:(异步线程连续发布了好几次进度后,UI线程才开始集中更新,导致进度条跳跃式前进了)
Thread is running 10 ----doInBackground发布进度
Thread is running 20 ----doInBackground发布进度
Thread is running 30 ----doInBackground发布进度
handleMessage
handleMessage
handleMessage
Thread is running 40
Thread is running 50

谢谢
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
tiantangniao232
Android 80,490 成员 91,375 内容