2

This question is related to how-do-you-check-the-internet-connection-in-android.

@William solution heelps me a lot, but on limited connection it`s not working.

I have connected to a wireless modem, but this modem is not connected to the internet.

According to the android docs this function should do all the work, but I think the data that is checked by function is only between android -> modem, and not android -> webservice (internet).

The @William code is:

final ConnectivityManager conMgr =  (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
     //notify user you are online
} else {
     //notify user you are not online
} 

What can I do to check the internet conectivity? I missing something?

Community
  • 1
  • 1
  • 1
    I think as an alternative you could make an actual HTTP request to a remote URL and see if it is successful. – Nachi Jun 07 '13 at 14:00
  • @Nachi this was my first guest, but I thought it would take too long to get the HTTP answer, but the response is instantly! Post you comment as an answer that I can accept as a right one. Thanks – Elcio Arthur Cardoso Jun 07 '13 at 14:22

2 Answers2

1

I think as an alternative you could make an actual HTTP request to a remote URL and see if it is successful.

Nachi
  • 4,218
  • 2
  • 37
  • 58
0

try this

  public static boolean isInternetConnection(Context mContext) {
    final ConnectivityManager connMgr = (ConnectivityManager) mContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    final android.net.NetworkInfo wifi = connMgr
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    final android.net.NetworkInfo mobile = connMgr
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (wifi.isAvailable()
            && wifi.getState() == NetworkInfo.State.CONNECTED) {
        return true;
    } else if (mobile.isAvailable()
            && mobile.getState() == NetworkInfo.State.CONNECTED) {
        return true;
    } else {
        return false;
    }
}

public static boolean isWifi(Context mContext) {
    final ConnectivityManager connMgr = (ConnectivityManager) mContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    final android.net.NetworkInfo wifi = connMgr
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (wifi.isAvailable()) {
        return true;
    } else {
        return false;
    }
}

public static boolean isOtherNetwork(Context mContext) {
    final ConnectivityManager connMgr = (ConnectivityManager) mContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    final android.net.NetworkInfo mobile = connMgr
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (mobile.isAvailable()) {
        return true;
    } else {
        return false;
    }
}

add permission in your menifest

        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
rahul
  • 2,613
  • 8
  • 32
  • 55