我必须在我的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");
但令人惊讶的是,我有一些时间。我无法理解原因
这是我的doInBackground
方法:
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;
}
请问有什么问题!