0

I find a solution to check internet access but it didn't works on all devices

How to check internet access on Android? InetAddress never times out

public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

This solution works on Samsung galaxy tab 2 1.10 (Android 4.1.2).

But not on HTC wildfire (Android 2.3.5)

On the second device netInfo is always null

Thanks for your help

Community
  • 1
  • 1
Ajouve
  • 9,735
  • 26
  • 90
  • 137

2 Answers2

1

as per below Documentation return null is becasue of NO Default network (Data network) available.

Looks like only isConnected() can help you to identify will it connected or Not. i tried on 4 Phones (Sony Ericson, samsung (2 phones ) , Nexus Lg) it shows the perfect result as required.

public NetworkInfo getActiveNetworkInfo ()

Added in API level 1 Returns details about the currently active default data network. When connected, this network is the default route for outgoing connections. You should always check isConnected() before initiating network traffic.

This may return null when there is no default network.

Returns a NetworkInfo object for the current default network or null if no network default network is currently active This method requires the call to hold the permission ACCESS_NETWORK_STATE.

Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95
kedark
  • 244
  • 1
  • 7
0

This is not enough...

With ConnectivityManager, you know if your phone has a connectivity available (wifi or 4G). But you aren't sure if you really have internet access (you can be connected to a wifi spot with limited connection or need an identification.

After testing the connection (using ConnectivityManager), you have to ping a web site.

Here is the best answer that explain how and what to do : Ping a Server

Christian
  • 748
  • 7
  • 23