确定网络连接带宽(速度)WiFi和移动数据 [英] Determine Network Connection Bandwidth (speed) wifi and mobile data

查看:293
本文介绍了确定网络连接带宽(速度)WiFi和移动数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在kbps或Mbps的网络连接带宽。如果该装置被连接到无线则它应该返回网络带宽(速度),以及移动数据

这将返回无线capablity率,但我想确切的数据传输速率。

 公共字符串getLinkRate()
{
    WifiManager WM =(WifiManager)getSystemService(Context.WIFI_SERVICE);
    WifiInfo无线= wm.getConnectionInfo();
    返回的String.Format(%d个Mbps的,wi.getLinkSpeed​​());
}
 

解决方案

您不能只是查询这些信息。您的上网速度被确定,并通过您的ISP控制,而不是由你的网络接口或路由器。

所以,你可以得到你的(目前的)连接速度的唯一方法是通过从足够近的位置下载文件和计时需要多长时间来检索文件。例如:

 静态最后弦乐FILE_URL =htt​​p://www.example.com/speedtest/file.bin;
静态最后长FILE_SIZE = 5 * 1024 * 8; // 5MB的千位

长mStart,缝缝补补;
语境mContext;
URL mUrl =新的URL(FILE_URL);
HttpURLConnection的的mCon =(HttpURLConnection类)mUrl.openConnection();
mCon.setChunkedStreamingMode(0);

如果(mCon.getResponse code()== HttpURLConnection.HTTP_OK){
    mStart =新的日期()的getTime()。

    输入的InputStream = mCon.getInputStream();
    文件F =新的文件(mContext.getDir(温度,Context.MODE_PRIVATE),file.bin);
    FileOutputStream中FO =新的FileOutputStream(F);
    INT read_len = 0;

    而((read_len = input.read(缓冲液))大于0){
        fo.write(缓冲液,0,read_len);
    }
    fo.close();
    MEND =新的日期()的getTime()。
    mCon.disconnect();

    返回FILE_SIZE /((MEND  -  mStart)/ 1000);
}
 

这code,当悦目修改(需要mContext是一个有效的范围内),并从的AsyncTask 或工作线程中执行,会下载一个远程文件,并返回该文件被下载在Kbps的速度。

I want to get Network Connection Bandwidth in kbps or mbps. if the device is connected to wifi then it should returns the network bandwidth(speed) as well as mobile data.

it will returns wifi capablity rate but i want exact data transfer rate.

public String getLinkRate() 
{
    WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
    WifiInfo wi = wm.getConnectionInfo();
    return String.format("%d Mbps", wi.getLinkSpeed());
}

解决方案

You can't just query for this information. Your Internet speed is determined and controlled by your ISP, not by your network interface or router.

So the only way you can get your (current) connection speed is by downloading a file from a close enough location and timing how long it takes to retrieve the file. For example:

static final String FILE_URL = "http://www.example.com/speedtest/file.bin";
static final long FILE_SIZE = 5 * 1024 * 8; // 5MB in Kilobits

long mStart, mEnd;
Context mContext;
URL mUrl = new URL(FILE_URL);
HttpURLConnection mCon = (HttpURLConnection)mUrl.openConnection();
mCon.setChunkedStreamingMode(0);

if(mCon.getResponseCode() == HttpURLConnection.HTTP_OK) {
    mStart = new Date().getTime();

    InputStream input = mCon.getInputStream();
    File f = new File(mContext.getDir("temp", Context.MODE_PRIVATE), "file.bin");
    FileOutputStream fo = new FileOutputStream(f);
    int read_len = 0;

    while((read_len = input.read(buffer)) > 0) {
        fo.write(buffer, 0, read_len);
    }
    fo.close();
    mEnd = new Date().getTime();
    mCon.disconnect();

    return FILE_SIZE / ((mEnd - mStart) / 1000);
}

This code, when sightly modified (you need mContext to be a valid context) and executed from inside an AsyncTask or a worker thread, will download a remote file and return the speed in which the file was downloaded in Kbps.

这篇关于确定网络连接带宽(速度)WiFi和移动数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆