如何获得下载时间

时间:2011-12-18 10:15:01

标签: 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 : "+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 个答案:

没有答案