1

我如何检查互联网是否有。但我找到了这段代码,但是当 wifi 连接时它返回 true,但我知道没有互联网。不能访问网络。

 public boolean chechInternet_con(){
    ConnectivityManager connec = (ConnectivityManager)
    context.getSystemService(Context.CONNECTIVITY_SERVICE);
    android.net.NetworkInfo wifi =connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    // Here if condition check for wifi and mobile network is available or not.
    // If anyone of them is available or connected then it will return true, otherwise false;

    if (wifi.isConnected() || mobile.isConnected()) {
        return true;
    } 
    return false;

}
4

3 回答 3

1

试试这个,

public boolean isInternetOn(Context ctx) {
        this.mContext = ctx;
        ConnectivityManager Connect_Manager = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        State connected = NetworkInfo.State.CONNECTED;
        State connecting = NetworkInfo.State.CONNECTING;
        State disconnected = NetworkInfo.State.DISCONNECTED;

        State info0 = Connect_Manager.getNetworkInfo(0).getState();
        State info1 = Connect_Manager.getNetworkInfo(1).getState();

        // ARE WE CONNECTED TO THE NET
        if (info0 == connected || info0 == connecting || info1 == connecting
                || info1 == connected) {

            Log.d("Internet", "Connected");
            return true;
        } else if (info0 == disconnected || info1 == disconnected) {
            Log.d("Internet", "DisConnected");
            return false;
        }
        return false;
    }
于 2012-07-26T05:11:27.880 回答
0

见下面的代码

    public static boolean checkConnection(Context context) {

    final ConnectivityManager mConnectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    final NetworkInfo netInfo = mConnectivityManager.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    } else
        return false;
}
于 2012-07-26T05:14:10.737 回答
0

因此,它连接到 wi-fi 网络但无法访问互联网。对?

尝试 ping 8.8.8.8 或 google.com 以了解是否可以访问 Internet。 将对您有所帮助。

于 2012-07-26T05:24:28.473 回答