-2

I able to check Mobile is connected to wifi router but how check router accessing internet or not ?if phone is connected to router then it returns true but not check for internet access plz help...

public boolean isInternetAvailable() {

    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].(info[i].getState() == NetworkInfo.State.CONNECTED)

            if(info[i]!=null && info[i].isAvailable()  &&  info[i].isConnected())
            {


                try {
                    if (InetAddress.getByName("https://www.google.co.in").isReachable(3000))
                    {
                        return true;
                    }
                    else
                    {
                        return  false;
                        // host not reachable
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Log.i("Network Info", info[i].toString());
                    return true;
                }
        }
        return false;
    }
    return false;
}

}

  • Possible duplicate of [How to check internet access on Android? InetAddress never timeouts...](http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts) – Selvin Oct 27 '15 at 12:32

1 Answers1

-2

Use this function. It will check the actual state of the internet connection is running or not.

Note This function will ping to the google public DNS server to check the connection. So its doesn't guarantee to give correct result in case when DNS server down. While google DNS server is almost online everytime so we can trust it.

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;
}
Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55