2013-04-03 59 views
3

我想更新一個進度對話框來增加類似1-100%的東西。但是,沒有任何增加。它總是隻表示0.雖然,我能看到我的Log Cat中打印出的數字,並且當它應該在100之後時,所有數據都會結束。Android AsyncTask publishProgress沒有更新對話框

有沒有人看到我缺少的東西?預先感謝您的幫助。

private class DownLoadSigTask extends AsyncTask<String, Integer, String> { 
    private final ProgressDialog dialog = new ProgressDialog(NasStorageNasList.this); 
    // Set the fileName and filesize to be used later 
    String fileName = (String) recordItem.get(mPresentDown).get("name"); 
    String filesize = (String) recordItem.get(mPresentDown).get("filesize"); 

    // can use UI thread here 
    @Override 
    protected void onPreExecute() { 
     this.dialog.setMessage("Downloading " + fileName + " from the server. Please wait."); 
     this.dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     this.dialog.setCancelable(false); 
     this.dialog.setCanceledOnTouchOutside(false); 
     this.dialog.show(); 
    } 

    // automatically done on worker thread (separate from UI thread) 
    @Override 
    protected String doInBackground(final String... args) { 
     // Here is where we need to do the downloading of the 


     try { 
      File destDir = new File(LOCALDOWNROOT); 
      if (!destDir.exists()) { 
       destDir.mkdirs(); 
      } 

      File outputFile = new File(destDir, fileName); 
      Log.e("What is the filename path to download? ", REMOTEDOWNROOT + DATADIRECTORY + fileName); 
      InputStream fis = sardine.get(REMOTEDOWNROOT + DATADIRECTORY + "/" 
        + fileName.replace(" ", "%20")); 

      FileOutputStream fos = new FileOutputStream(outputFile); 
      byte[] buffer = new byte[1444]; 
      Log.e("What is the length of the File Size? ", filesize); 


      long total = 0; 
      int byteread = 0; 
      while ((byteread = fis.read(buffer)) != -1) { 
       downloadTotal += byteread; 
       // Here is the WHILE LOOP that we will want to give the user 

       publishProgress((int)(total*100/Long.parseLong(filesize))); 
       Log.e("CurrentAmount Downloaded: ", String.valueOf((int)(downloadTotal*100/Long.parseLong(filesize)))); 
       // How can I tell the UI the downloaded percentage? 
       // So, far I am updating the log cat...but nothing for the user. 


       fos.write(buffer, 0, byteread); 
      } 
      fis.close(); 
      fos.close(); 


     } catch (IOException e) { 
      e.printStackTrace(); 
     } 


     // Just Return null because the void task 
     return null; 
    } 
    // add in a progress bar update 
    @Override 
    protected void onProgressUpdate(Integer...progress) { 
     this.dialog.setProgress(progress[0]); 
    } 

    // can use UI thread here 
    @Override 
    protected void onPostExecute(final String errMsg) { 
     if (this.dialog.isShowing()) { 
      this.dialog.dismiss(); 
     } 

      Toast.makeText(NasStorageNasList.this, "File Download Done!", 
        Toast.LENGTH_SHORT).show(); 

    } 
}// end DownloadSigTask 

回答

6

您有「錯字」錯誤。您需要更換您的publishProgress()方法總計變量與下載總計

publishProgress((int) (downloadTotal * 100/Long.parseLong(filesize))); 

變量只分配到零循環之前,並沒有改變到其他值。所以你執行的分區0/filesize將始終爲0.

+1

謝謝!你100%正確! – Rob