2014-02-27 51 views
1

我正在开发一个应用程序,下载文件并显示2个进度条,第一个用于当前下载文件,第二个用于基于文件数目的总进度。通过AsyncTask更新双进度条

我使用DoubleProgressBar库在我的应用程序:

我成功更新第一进度,但坚持:第二个。

这里是我的的AsyncTask类代码:

private DoubleProgressDialog pDialog; 

    class DownloadFileFromURL extends AsyncTask<String, Integer, String> { 
     Context mContext; 

     public DownloadFileFromURL(Context ctx) { 
      // TODO Auto-generated constructor stub 
      this.mContext = ctx; 
     } 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      showDialog(CUSTOM_PROGRESS_DIALOG); 
     } 

     /* Downloading file in background thread */ 
     @Override 
     protected String doInBackground(String... f_url) { 

      InputStream input = null; 
      OutputStream output = null; 
      HttpURLConnection connection = null; 

      try { 
       URL url = new URL(f_url[0]); 
       connection = (HttpURLConnection) url.openConnection(); 
       connection.connect(); 
       // getting file length 
       int fileLength = connection.getContentLength(); 
       for (int i = 0; i <= ArrayOfFiles.length; i++){ 
       File f = new File(Environment.getExternalStorageDirectory() + "/Folder/", ArrayOfFiles[i]); 

       // input stream to read file - with 8k buffer 
       input = new BufferedInputStream(url.openStream(), 8192); 
       // Output stream to write file 
       output = new FileOutputStream(f); 

       byte data[] = new byte[8192]; 

       long total = 0; 
       int count; 
       int EntireProgress = 0; 
       while ((count = input.read(data)) != -1) { 
        // allow canceling with back button 
        if (isCancelled()) { 
         input.close(); 
         return null; 
        } 
        total += count; 
        // publishing the progress.... 
        if (fileLength > 0) // only if total length is known 
        publishProgress((int)(total * 100/fileLength)); 
        output.write(data, 0, count); 

         /*Here is my trouble, the 2nd ProgressBar is updating as the same of the first one, I need the 2nd one to update itself slowly till all files get downloaded*/ 
        int CurrentProgress = pDialog.getProgress(); 
        pDialog.setSecondaryProgress(CurrentProgress); 
        publishProgress(CurrentProgress); 

       } 

      } catch (Exception e) { 
       return e.toString(); 
      } finally { 
       try { 
        if (output != null) 
         output.close(); 
        if (input != null) 
         input.close(); 
      } 
       } catch (IOException ignored) { 
       } 

       if (connection != null) 
        connection.disconnect(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      // dismiss the dialog after the file was downloaded 
      dismissDialog(CUSTOM_PROGRESS_DIALOG); 

      if (result != null) 
       Toast.makeText(mContext,"Download error: " + result, Toast.LENGTH_LONG).show(); 
      else 
       Toast.makeText(mContext,"File downloaded", Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     protected void onProgressUpdate(Integer... progress) { 
      super.onProgressUpdate(progress); 
      // if we get here, length is known, now set indeterminate to false 
      pDialog.setIndeterminate(false); 
      pDialog.setMax(100); 
      pDialog.setProgress(progress[0]); 
     } 

    } 

我也在我的活动课使用这种方法:

@Override 
     protected Dialog onCreateDialog(int id) { 
      switch (id) { 
      case CUSTOM_PROGRESS_DIALOG: 
       pDialog = new DoubleProgressDialog(NetworkActivity.this); 
       pDialog.setMessage("Downloading file. Please wait..."); 
       pDialog.setMax(100); 
       pDialog.setIndeterminate(true); 
       pDialog.setCancelable(false); 
       pDialog.setButton("Cancel", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int i) { 
         dialogInterface.cancel(); 
        } 
       }); 
       pDialog.show(); 
       return pDialog; 
      default: 
       return null; 
      } 
     } 

任何想法?

+0

'publishProgress(CurrentProgress);'是什么方法呢?另外:您只处理一个文件,但想要根据多个文件更新第二个进度? – cosmincalistru

+0

尝试将'pDialog.setSecondaryProgress(CurrentProgress);'移动到'onProgressUpdate(Integer ... progress)'。由于您是从非UI线程更新UI,可能是问题 –

+0

@cosmincalistru,可以从doInBackground(Params ...)调用此方法在后台计算仍在运行时在UI线程上发布更新。每次调用此方法都会触发UI线程上onProgressUpdate(Progress ...)的执行。如果任务已被取消,onProgressUpdate(Progress ...)将被调用。 将更新我的代码下载多个文件。 – blueware

回答

1

第一部分是将pDialog.setSecondaryProgress移动到onProgressUpdate(Integer... progress)方法。
通过将每个下载任务的次要进度设置为CurrentProgress,并将其设置为pDialog.getProgress();,您也可以重置次要进度。因此,下载完成后,第二个进度将始终重置。

编辑:

// publishing the progress.... 
if (fileLength > 0) // only if total length is known 
    publishProgress((int)(total * 100/fileLength), pDialog.getSecondaryProgress()); 

(...) 

int CurrentProgress = pDialog.getProgress(); 

// do not update secondary progress here 
// pDialog.setSecondaryProgress(CurrentProgress); 
int secondaryProgress = (CurrentProgress + 100 * i)/ArrayOfFiles.length; 
publishProgress(CurrentProgress, secondaryProgress); 

而且onProgressUpdate

@Override 
protected void onProgressUpdate(Integer... progress) { 
    super.onProgressUpdate(progress); 

    (...)  

    pDialog.setProgress(progress[0]); 
    pDialog.setSecondaryProgress(progress[1]); 

} 
+0

我想这一点,但第二个进度很快有人填补,停留在100%:( – blueware

+0

你第二进度设置什么最大? –

+0

如果我得到你的意思,我想通过XML?如果是的话,我没有设定最高对于我的两个ProgressBars,如果没有,那么在我的代码中,只有1个进度有原发性和继发性的进步等等,我SETMAX我pDialog仅 – blueware

1

如果不设置你DownloadFileFromUrl主类之外,我建议这样的事情

int CurrentProgress = pDialog.getProgress(); 
int secondaryProgress = (CurrentProgress + 100 * id_treated_file)/number_of_files; 
// id_treated_file - 0, 1, 2, ... , number_of_files - 1 
pDialog.setSecondaryProgress(CurrentProgress); 
// secondaryProgress will be progress[1] in your onProgressUpdate method 
publishProgress(CurrentProgress, secondaryProgress); 

onProgressUpdate方法应该看起来像这样:

@Override 
protected void onProgressUpdate(Integer... progress) { 
    super.onProgressUpdate(progress); 
    // if we get here, length is known, now set indeterminate to false 
    pDialog.setIndeterminate(false); 
    pDialog.setMax(100); 
    pDialog.setProgress(progress[0]); 
    pDialog.setSecondaryProgress(progress[1]); 
} 

编辑
或者你可以尝试

pDialog.setSecondaryProgress((progress[0] + 100 * id_treated_file)/number_of_files); 
+0

该行:pDialog.setSecondaryProgress(progress [1]);让我ArrayIndexOutOfBound因为只有0索引值,但是,如果我修改它作为这样的:pDialog.setSecondaryProgress(进展[0]/NumOfFiles); ,它的工作原理,但它重置本身的每一个文件的下载任务 – blueware

+0

看到编辑解决方案 – cosmincalistru

+0

仍然没有工作:( – blueware