【发布时间】: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