1

我可以用这个检查设备是否连接到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;
}

但是,我想检查 wifi 连接速度,比如 Ping。我想根据这个 ping 号码来设置一些变量。像这样的东西,

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

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

4

3 回答 3

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
    }
于 2012-05-10T07:20:47.593 回答
0

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

于 2012-05-10T06:57:00.427 回答
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;
    }

}
于 2012-05-10T07:03:11.220 回答