我必须在我的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;
}
请问有什么问题!
答案 0 :(得分:0)
看起来你正在为两个日志语句使用相同的日期对象。在“下载完成”日志声明之前添加:
dt = new Date();
顺便说一下,Date的所有方法(getSeconds,getHours等)都会被删除。您可以通过在下载开始时执行System.currentTimeMillis()并在结束时再次进行减法来获得时序。这是总持续时间(以毫秒为单位),您可以根据需要将其转换为小时/分钟/秒。