2011-12-03 28 views
1

私はAndroidアプリケーションでダウンロードする時間を得なければならないので、ダウンロードの開始日と終了日を決定する必要があります。 私は最初とdoInBackgroundの最後にこれらの行を追加しました:ダウンロードの開始時刻と終了時刻を取得する

Log.v("Download_INFO","i begin downloading at : "+" "+dt.getHours()+"h "+dt.getMinutes()+"min " 
         +dt.getSeconds()+"sec"); 

Log.v("Download_INFO","i complete downloading at : "+" "+dt.getHours()+"h "+dt.getMinutes() 
         +"min "+dt.getSeconds()+"sec"); 

しかし、驚きは、私はいくつかの時間を持っていたということです。

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() + "/" 
         + "downloadTest.jpg"); 
       URL url = new URL(aurl[0]); 
       URLConnection conexion = url.openConnection(); 
       conexion.connect(); 
       int lenghtOfFile = conexion.getContentLength(); 
       Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 
       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); 
       } 

       Log.v("Download_INFO","i complete downloading at : "+" "+dt.getHours()+"h "+dt.getMinutes() 
         +"min "+dt.getSeconds()+"sec"); 
       output.flush(); 
       output.close(); 
       input.close(); 
       System.out.println(currentDateTimeString); 
      } 

     } catch (Exception e) { 
      Log.d("ImageManager", "Error: " + e); 
     } 

     return null; 
    } 

問題は何をしてくださいます。私は、これは私のdoInBackground方法である理由に

を理解することはできません!

答えて

0

両方のログステートメントで同じ日付オブジェクトを使用しているようです。あなたの「完全なダウンロード」ログステートメントの前にこれを追加します。

dt = new Date(); 

ところで、日付のすべてのそれらのメソッド(getSeconds、次に、getHoursなど)は廃止予定されています。ダウンロードの開始時にSystem.currentTimeMillis()を実行し、最後にもう一度、減算するだけで、タイミングを取得できます。これはミリ秒単位の合計時間で、必要に応じて時間/分/秒に変換できます。

+0

素晴らしいと思います。それはちょうど "System.currentTimeMillis" – Linconnue55

関連する問題