2011-12-18 18 views
0

我需要在啓動我的android應用程序時獲得下載時間。所以,我加了2 Log和System.currentTimeMillis()。但是,我找不到放置這些工具的確切位置。如何獲得下載時間

要獲得起始時間,我加了這一點:

period=System.currentTimeMillis(); 
System.out.println("Début téléchargement : "+System.currentTimeMillis()); 

成Ser結束的時候,我加了這一點:

System.out.println("Fin téléchargement : "+System.currentTimeMillis()); 
period=(System.currentTimeMillis()-period); 
System.out.println("La durée de téléchargement : "+period+"ms"); 

這是所有下載文件的代碼:

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 { 

       File vSDCard = null; 
       if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 
        vSDCard = Environment.getExternalStorageDirectory(); 
        File vFile = new File(vSDCard.getParent() + "/" + vSDCard.getName() + "/" 
          + "image.jpg"); 
        URL url = new URL(aurl[0]); 
        URLConnection conexion = url.openConnection(); 
        int lenghtOfFile = conexion.getContentLength(); 
        Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 
             conexion.connect(); 

        InputStream input = new BufferedInputStream(url.openStream()); 
        OutputStream output = new FileOutputStream(vFile); 
        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) { 
       Log.d("ImageManager", "Error: " + e); 
      } 

      return null; 
     } 




     protected void onProgressUpdate(String... progress) { 
      Log.d("ANDRO_ASYNC", progress[0]); 
      ProgressDialog.setProgress(Integer.parseInt(progress[0])); 
     } 
     @Override 
     protected void onPostExecute(String unused) { 
      dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
     } 
    } 

我需要你的建議。

回答

0

打開URL連接之前的第一個塊或剛剛從輸入流開始reding之前。

連接關閉後或剛剛循環後的第二個塊。

這有什麼困難嗎?我錯過了什麼嗎?

+0

非常感謝你,我在'conexion.connect()'之前和之後設置了第一個和第二個塊。我將結果與IDM進行了比較。它給出了相同的結果。 – Linconnue55 2011-12-18 11:03:47

+0

如果您想要更精確,您可以使用納秒時間。 – 2011-12-18 12:11:19

相關問題