Android 如何获取下载时间
我需要在启动android应用程序时获得下载时间。因此,我添加了2个Log和System.currentTimeMillis。但是,我找不到放置这些工具的确切位置 为了获得开始时间,我添加了以下内容:Android 如何获取下载时间,android,time,download,Android,Time,Download,我需要在启动android应用程序时获得下载时间。因此,我添加了2个Log和System.currentTimeMillis。但是,我找不到放置这些工具的确切位置 为了获得开始时间,我添加了以下内容: period=System.currentTimeMillis(); System.out.println("Début téléchargement : "+System.currentTimeMillis()); System.out.println("Fin téléchargement
period=System.currentTimeMillis();
System.out.println("Début téléchargement : "+System.currentTimeMillis());
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");
为了确定结束时间,我添加了以下内容:
period=System.currentTimeMillis();
System.out.println("Début téléchargement : "+System.currentTimeMillis());
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);
}
}
我需要您的建议。在打开URL连接之前或刚开始从输入流重新拨号之前,请先阻止 连接关闭后或循环刚结束后的第二个块
这有什么难的?我遗漏了什么吗?非常感谢,我在“conexion.connect;”前后设置了第一个和第二个区块我将结果与IDM进行了比较。它给出了同样的结果。如果你想要更精确的话,你可以使用纳米时间。