获取NetworkInfo返回isconnectedtoprovisioningnetwork()

时间:2015-01-09 12:04:24

标签: android wifi

您好我的应用已连接到无法访问互联网的WIFI。如何获得isconnectedtoprovisioningnetwork()的返回?我希望得到它是真还是假,并将其与我的代码联系起来。

这是我的代码:

public static boolean isConnected(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null)
            for (int i = 0; i < info.length; i++)
                if (info[i].getState() == NetworkInfo.State.CONNECTED && info[i] != null && info[i].isAvailable() && info[i].isConnected() )

                    return true;
    }
    return false;
}

2 个答案:

答案 0 :(得分:1)

public static boolean checkInternetConnection(Context context) {

    ConnectivityManager conMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    // ARE WE CONNECTED TO THE NET
    if (conMgr.getActiveNetworkInfo() != null

    && conMgr.getActiveNetworkInfo().isAvailable()

    && conMgr.getActiveNetworkInfo().isConnected()) {

        return true;

    } else {
        return false;
    }
}

答案 1 :(得分:1)

此代码会告诉您是否已连接到WIFI。

public boolean isConnection(Context ctx) {
        ConnectivityManager connManager = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (mWifi!=null && mWifi.isConnected()) {
            return true;
        }
        return false;
    }

不要忘记添加权限。

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />