-1

Am checking internet present or not by following method.

public boolean isConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netinfo = cm.getActiveNetworkInfo();
    if (netinfo != null && netinfo.isConnectedOrConnecting()) {
        NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        return (mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting());
    } else
        return false;}

But when connected to wifi,but no working internet is there then also this method gives true.

Thanks in advance, please help me out

Bincy Baby
  • 3,941
  • 5
  • 36
  • 62
  • 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) – Bincy Baby May 03 '16 at 01:22

6 Answers6

1

this may be help you

public boolean isConnectingToInternet(){
    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].getState() == NetworkInfo.State.CONNECTED)
                  {
                      return true;
                  }

      }
      return false;
}
Amit Basliyal
  • 840
  • 1
  • 10
  • 28
1

Try this :

public static boolean Check_Internet_Connectivity(Context _context)
    {
        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].getState() == NetworkInfo.State.CONNECTED)
                    {
                        return true;
                    }

            }
        }
        Toast.makeText(_context, "Internet Connection is not available", Toast.LENGTH_SHORT).show();
        return false;
    }
M.Muzammil
  • 643
  • 9
  • 18
0

use obj.isAvailable() to check network availability.

You can find More Here NetworkInfo.

Umesh Chauhan
  • 1,460
  • 1
  • 17
  • 26
0

This is how internet is checked:-

100% working code

private AlertDialog internetDialog, gpsAlertDialog;
  public BroadcastReceiver internetConnectionReciever = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                ConnectivityManager connectivityManager = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo activeNetInfo = connectivityManager
                        .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                NetworkInfo activeWIFIInfo = connectivityManager
                        .getNetworkInfo(connectivityManager.TYPE_WIFI);

                if (activeWIFIInfo.isConnected() || activeNetInfo.isConnected()) {
                    removeInternetDialog();
                } else {
                    if (isNetDialogShowing) {
                        return;
                    }
                    showInternetDialog();
                }
            }
        };
Nik Patel
  • 627
  • 7
  • 21
0

Ping any real host (sample 8.8.8.8)

InetAddress.getByName("8.8.8.8").isReachable(timeOut)
Alexandr Sulimov
  • 1,894
  • 2
  • 24
  • 48
-1

if you happen to loading page by webview , you can check it also for calling for custom 404 error page :

webView.loadUrl(urlhere);    
            webView.setWebViewClient(new WebViewClient() {
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    webView.loadUrl("file:///android_asset/new404.html");

                }
            });
ganero
  • 91
  • 1
  • 2
  • 16
  • 1
    i am just trying to help, by checking the error if using webview you know the internet is not active. , i hope the one who downvote me got bad day. – ganero Dec 12 '15 at 09:44