如何检查工作的无线网络是否存在?

时间:2016-05-11 12:06:22

标签: android networking connection android-wifi

public boolean isConnected(Context context) {
    connected=false;
    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (info != null && info.getState() == NetworkInfo.State.CONNECTED && info.isConnected()) {
           Log.e("link",new Gson().toJson(info.getDetailedState()));
            connected=true;
            Needle.onBackgroundThread().execute(new Runnable() {
                @Override
                public void run() {

                    try {
                        HttpURLConnection urlc = (HttpURLConnection) (new URL(mainDomain).openConnection());
                        urlc.setRequestProperty("User-Agent", "Test");
                        urlc.setRequestProperty("Connection", "close");
                        urlc.setConnectTimeout(1500);
                        urlc.connect();
                        connected=true;
                        Log.e("connection",new Gson().toJson(urlc.getResponseCode()));

                    } catch (IOException e) {
                        e.printStackTrace();
                        connected=false;
                    }
                }
            });
            return connected;
        }
    }

    return connected;
}

我使用过此代码。它的工作正常,但我的问题是我需要调用服务器。有没有其他方法来检查WiFi的速度或工作?

WifiManager wifiManager=(WifiManager)this.getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int speedMbps = wifiInfo.getLinkSpeed();

我使用上面的代码来检查速度,但它也没有正常工作。

请帮帮我。提前致谢

2 个答案:

答案 0 :(得分:1)

你可以尝试这个

// Script - JavaScript
function drawChart_Line(stationName) 
{
    @foreach (var item2 in Model.LineChart.Where(m => m.StationName == stationName)
    {
        <My Code...>
    }
}

答案 1 :(得分:0)

您可以使用以下代码:

ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();

这将告诉您设备是否已连接。