0

I try code

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

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
                      activeNetwork.isConnectedOrConnecting();

It checks WiFi or Mobile network open and close only. But I want to check with Internet access. Such as open WiFi, but can connect the Internet.

Kung
  • 33
  • 5
  • Check my answer here http://stackoverflow.com/questions/37430738/how-to-check-internet-connection-before-app-starts-and-while-it-is-running/37431259#37431259 – sandesh Jun 05 '16 at 13:18
  • Make a test http request to a website? – Ozgur Jun 05 '16 at 13:21

1 Answers1

1

I use a ping in Google to check the if the user have real internet connection, just be careful if your user is in China or any other country which Google is blocked.

public boolean isOnline() {

    Runtime runtime = Runtime.getRuntime();
    try {

        Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
        int     exitValue = ipProcess.waitFor();
        return (exitValue == 0);

    } catch (IOException e)          { e.printStackTrace(); } 
      catch (InterruptedException e) { e.printStackTrace(); }

    return false;
}
Ismael Di Vita
  • 1,806
  • 1
  • 20
  • 35