【问题标题】:Android - how to calculate the time needed to download a file?Android - 如何计算下载文件所需的时间?
【发布时间】:2018-03-18 18:57:58
【问题描述】:

在我的 Android 应用程序中,我使用 WIFI 链接速度来获取 WIFI 的速度 并在下载文件之前获取文件的长度内容 然后我试图在下载文件之前获得估计的下载时间 但是我得到的时间不正确我不知道为什么!

这是我在下载前估计时间的代码

  URL u = new URL(url);
  HttpURLConnection c = (HttpURLConnection) u.openConnection();
  c.setRequestMethod("GET");
  c.connect();
  contentLength = Long.parseLong(c.getHeaderField("Content-Length"));
  System.out.println("content"+contentLength);
  float contentLength_float=contentLength/(float)(1000*1000);//migabyte
  float speed=((float)(mActivity.speed_wifi()))/(float)8;//convert mbps(migabit) to migabyte ps
  float sec=contentLength_float/speed;//get the sec from m/m/s

和函数wifi速度()

     public int speed_wifi()
  {
   WifiManager mainWifi;
        mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = mainWifi.getConnectionInfo();
        int speed=0;
        if(wifiInfo.getBSSID()!=null)
         {
            speed=wifiInfo.getLinkSpeed();
         }
        return speed;
  }

【问题讨论】:

  • 我想在开始下载之前计算它,而不是之后。
  • 好吧,所以你可以得到 Wifi 的速度和文件的大小,只需划分它
  • 问题是当我这样做时,我得到的秒数比下载文件所需的实际时间少得多。我不知道为什么会这样。
  • 为此,您必须不断检查速度是否在变化。所以时间可能会增加

标签: java android download android-wifi


【解决方案1】:

您使用该功能获得的wifi链接速度是手机中wifi可以达到的最大速度,而不是实际速度。 在下载开始之前无法确定 wifi 速度。 您可以做的是,根据当前的下载速度开始显示下载开始时的估计时间。为此-

  1. 找出在一小段时间(例如 2 秒)内下载了多少数据,即 current_speed = data_downloaded/time(时间可以是 2 秒或任何您想要的时间)
  2. 现在预计时间将为 file_size/current_speed。

因此,通过这种方式,您可以在下载开始后 1 或 2 秒内开始显示预计时间。

【讨论】:

    【解决方案2】:

    使用 AysnTask

      InputStream input = null;
            OutputStream output = null;
            HttpURLConnection connection = null;
            try {
                URL url = new URL(sUrl[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();
    
                // expect HTTP 200 OK, so we don't mistakenly save error report
                // instead of the file
                if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                    return "Server returned HTTP " + connection.getResponseCode()
                            + " " + connection.getResponseMessage();
                }
    
                // this will be useful to display download percentage
                // might be -1: server did not report the length
                int fileLength = connection.getContentLength();
    
                // download the file
                input = connection.getInputStream();
                output = new FileOutputStream("/sdcard/file_name.extension");
    
                byte data[] = new byte[4096];
                long total = 0;
                int count;
                while ((count = input.read(data)) != -1) {
                    // allow canceling with back button
                    if (isCancelled()) {
                        input.close();
                        return null;
                    }
                    total += count;
                    // publishing the progress....
                    if (fileLength > 0) // only if total length is known
                        publishProgress((int) (total * 100 / fileLength));
                    output.write(data, 0, count);
                }
            } catch (Exception e) {
                return e.toString();
            } finally {
                try {
                    if (output != null)
                        output.close();
                    if (input != null)
                        input.close();
                } catch (IOException ignored) {
                }
    
                if (connection != null)
                    connection.disconnect();
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多