2014-02-27 79 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