如何在线程列表视图中的进度条中设置下载进度?

时间:2014-03-13 10:31:34

标签: android multithreading listview progress-bar

我正在使用下载管理器下载文件。我想在列表视图中显示进度条,显示下载状态。我正在计算线程中的进度。当我尝试从线程设置进度时它是没有显示进展。为了取得进展,我该怎么做? 这是我的代码

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) {
        }
    }


};

1 个答案:

答案 0 :(得分:1)

您应该使用AsyncTask,实现onProgressUpdate()