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");

但令人惊讶的是,我有一些时间。我无法理解原因

这是我的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;
    }

请问有什么问题!

4

1 回答 1

0

看起来您对两个日志语句使用相同的日期对象。在“下载完成”日志语句之前添加:

dt = new Date();

顺便说一句,Date 的所有这些方法(getSeconds、getHours 等)都被贬低了。您只需在下载开始时执行 System.currentTimeMillis() 并在结束时再次执行并减去即可获得时间。这是以毫秒为单位的总持续时间,您可以根据需要将其转换为小时/分钟/秒。

于 2011-12-03T14:02:39.867 回答