我正在使用下载管理器下载一个文件。我想在列表视图中显示进度条,显示下载状态。我正在计算线程中的进度.When我试图设置线程的进度它没有显示progress.What为了获得进度我应该做什么?以下是我的代码
public Runnable updateprogress = new Runnable() {
public void run() {
try {
Log.v("updateprogress in thread",""+killthread);
if(killthread==false)
{
DownloadManager downloadmanager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(download_id);
Cursor cursor = downloadmanager.query(q);
if (cursor.moveToFirst()) {
FileDownloading.setDownload_status(cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)));
long fileSize = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
long bytesDL = cursor.getLong(cursor.getColumnIndex(DownloadManager
.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int progress=(int) ((bytesDL * 100.0f) / fileSize);
FileDownloading.setDownload_progress((int) ((bytesDL * 100.0f) / fileSize));
if(fileSize!=-1){
PR_BAR.setIndeterminate(false);
FileDownloading.setDownload_progress(progress);
PR_BAR.setProgress((int) ((bytesDL * 100.0f) / fileSize));
if((int) ((bytesDL * 100.0f) / fileSize)==100){
killthread=true;
if(PR_BAR!=null)
PR_BAR.setVisibility(View.GONE);
else
Log.e("progress bar","null");
}
}
else{
PR_BAR.setIndeterminate(true);
}
} else {
FileDownloading.setDownload_progress(0);
FileDownloading.setDownload_status(-1);
}
cursor.close();
mHandler.postDelayed(this, 5000);
}
else{
updateprogress.wait();
}
} catch (Exception e) {
}
}
};发布于 2014-03-13 19:08:48
您应该使用AsyncTask,实现onProgressUpdate()
https://stackoverflow.com/questions/22375762
复制相似问题