获取下载的开始时间和结束时间

发布于 2024-12-19 08:50:06 字数 2161 浏览 4 评论 0原文

我必须在我的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;
    }

请问有什么问题!

I have to get the time of download in my android application, that's why I need to determine the time of beginning and end of dowload to determine the period od download.
I added these line in the beginning and the end of 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");

But the surprise is that i had the some time. I can't understand the reason

this is my doInBackground method :

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;
    }

what's the problem please !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野侃 2024-12-26 08:50:06

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

dt = new Date();

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

It looks like you're using the same date object for both log statements. Add this before your "download complete" log statement:

dt = new Date();

By the way, all those methods (getSeconds, getHours, etc) of Date are depricated. You can get the timing simply by doing a System.currentTimeMillis() at the beginning of the download and again at the end and subtracting. This is the total duration in milliseconds which you can convert into hours/min/seconds if you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文