1

In my Android application, I am using WIFI link speed to get the speed of the WIFI and get the length content of a file before downloading the file and then I am trying to get the esitmated time of download before downloading the file but the time I get is incorrect I don't know why !

that is my code to estimate the time before downloading

  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

and function wifi speed ()

     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;
  }
Cœur
  • 37,241
  • 25
  • 195
  • 267
AerRayes
  • 69
  • 3
  • 12

2 Answers2

4

The wifi link speed you get by using that function is the maximum speed that can be achieved by the wifi in the phone, it is not the actual speed. There is no way of determining the wifi speed before the download starts. What you can do is that, start showing the estimated time as the download is started based on the current download speed. For this -

  1. find out how much data is downloaded in a small chunk of time like 2 sec which will be current_speed = data_downloaded/time (time can be 2 sec or anything you want)
  2. Now the estimated time will be file_size/current_speed.

So in this way you can start showing the estimated time just 1 or 2 seconds after the download is started.

Somil
  • 481
  • 3
  • 8
1

use 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();
        }
Shyam
  • 6,376
  • 1
  • 24
  • 38