如何检查设备连接的wifi上网速度?

时间:2012-05-10 06:52:14

标签: android networking connection wifi

我可以通过此检查设备是否连接到wifi。

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) activity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

但是,我想检查一下类似Ping的wifi连接速度。我想基于此ping号来设置一些变量。像这样的东西,

public int internetspeed(){
    checking...
    return speed;
}

有人可以给我教程或示例吗?

3 个答案:

答案 0 :(得分:3)

此代码段将为您完成工作

    WifiManager wifiManager = Context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo != null) {
        Integer linkSpeed = wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS
    }

答案 1 :(得分:0)

在后台下载文件并每秒测量数据。

答案 2 :(得分:0)

这是我用来测试连接的完整类。对于ping问题,请参阅我的评论。

public class InternetCheck {

    Context mContext;
     public InternetCheck(Context mContext){
           this.mContext = mContext;
      }


    // CHECK FOR INTERNET METHOD
    public final boolean isInternetOn() {
        ConnectivityManager connec = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        // ARE WE CONNECTED TO THE NET
        if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
                || connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING
                || connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING
                || connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) {
            return true;
        } else if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
                || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
            return false;
        }
        return false;
    }

}