2011-02-24 66 views
2

這與我以前的帖子Problem with downloading multiple files using AsyncTask如何取回任務的完成狀態中的AsyncTask

我試圖下載兩個視頻文件,並在過程中表現出ProgressDialog。爲此,我使用AsyncTask。我想要第一次下載完成,釋放內存然後開始第二次下載。我寫了下面的代碼來實現這一點,但似乎從未開始第二次下載。

startDownload() { 
    DownloadFileAsync d1 = new DownloadFileAsync(); 
    d1.execute(videoPath+fileNames[0],fileNames[0]); 

    if(d1.getStatus()==AsyncTask.Status.FINISHED) { 
    d1 = null; 
    DownloadFileAsync d2 = new DownloadFileAsync(); 
    d2.execute(videoPath+fileNames[1],fileNames[1]); 
    } 
} 

有沒有一種方法,我可以拿回我的第一個任務的完成狀態&然後開始第二次?

以下是我DownloadFileAsync類的代碼:

class DownloadFileAsync extends AsyncTask<String, String, String> { 

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

    @Override 
    protected String doInBackground(String... aurl) { 
     int count; 

     try { 
      URL url = new URL(aurl[0]); 
      URLConnection conexion = url.openConnection(); 
      conexion.connect(); 

      File root = android.os.Environment.getExternalStorageDirectory(); 

      int lenghtOfFile = conexion.getContentLength(); 
      Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 

      InputStream input = new BufferedInputStream(url.openStream()); 
      OutputStream output = new FileOutputStream(root.getAbsolutePath() + "/videos/" + aurl[1]); 

      byte data[] = new byte[1024]; 

      long total = 0; 

      while ((count = input.read(data)) != -1) { 
       total += count; 
       publishProgress(""+(int)((total*100)/lenghtOfFile)); 
       output.write(data, 0, count);      
      } 
      output.flush(); 
      output.close();     
      input.close(); 

     } catch (Exception e) {} 
     return null; 

    } 
    protected void onProgressUpdate(String... progress) { 
     Log.d("ANDRO_ASYNC",progress[0]); 
     mProgressDialog.setProgress(Integer.parseInt(progress[0])); 
    } 

    @Override 
    protected void onPostExecute(String unused) { 
     dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
     tv.append("\n\nFile Download Completed!"); 
     sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));    
    } 
} 

回答

1

由於DKIT Android的建議,你可以從onPostExecute開始第二次下載,但前提是例如下載1是空

@Override 
protected void onPostExecute(String unused) 
{ 

    if (d2 == null) 
    { 
     d2 = new DownloadFileAsync(); 
     d2.execute(videoPath+fileNames[1],fileNames[1]);  
    }  
} 

如果你需要開始更多的下載,只寫方法,您的AsyncTask外,這將檢查下一次應該開始下載哪個下載。

1

從第一onPostExecute法內開始你的第二個下載。

此代碼:

if(d1.getStatus()==AsyncTask.Status.FINISHED) { 
d1 = null; 
DownloadFileAsync d2 = new DownloadFileAsync(); 
d2.execute(videoPath+fileNames[1],fileNames[1]); 

}

...執行第一的AsyncTask將執行一次,緊接着,因此它永遠是假的。如果你想要走這條路線,你需要在一個while循環中完成 - 這首先打破了任務異步的地步。

+0

但是在真正的應用程序中,我不得不下載幾個文件,而不僅僅是2.那麼,有沒有其他的方式來實現這個目標? – Sourav 2011-02-24 11:24:19

+0

我試着在while while(d1.getStatus()== AsyncTask.Status.RUNNING){...}放一個while循環,但整個應用程序停滯不前。所以,我想知道是否有更好的方法來做到這一點。 – Sourav 2011-02-24 11:35:31

+0

我在onPostExecute()方法中保留了第二次下載的代碼。但第二個任務繼續執行無數次。請告訴我如何實現5個連續任務的邏輯(下載) – Sourav 2011-02-24 11:49:29