-6

I want to check internet connection when device is connected to a network but that doesnt have internet connection. I want to know what is the best way for checking this

I asked this question because didn't find any accept answer is SO. here are some duplicate questions but there isn't one accept answer:

Java: Internet connectivity check via ping to google not working

Check for Active internet connection Android

also two questions that has accepted answer, the answers are not working fine:

Android check internet connection

Test Internet Connection Android

Community
  • 1
  • 1
Ahmad Vatani
  • 1,630
  • 4
  • 21
  • 34
  • To downvoter: Could you please explain your downvote. This is a perfectly legit question for some user cases. – f470071 Nov 05 '15 at 10:28
  • 1
    obviously, @f470071 because it is a duplicate of bazillion similar questions, here on SO ... and ahmad didn't do any research ... – Selvin Nov 05 '15 at 10:33
  • @Selvin , please read again the question. can you show me an accepted answer that works fine??? – Ahmad Vatani Nov 05 '15 at 11:10
  • 1
    I don't think you understand that it is impossible to reliably detect Internet access. No system can detect the difference between a server that is not responding (even though you have Internet access) and a connection that is blocked (no Internet). All the existing methods just try to ping a server which is likely to be available. – adelphus Nov 05 '15 at 19:13

2 Answers2

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

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

This way you can determine if you have internet access or not - no need to fight with ping request or something like that.

LilaQ
  • 349
  • 5
  • 19
  • One should not confuse network access and internet access. You can easily be on a WiFi with no outside connection. – f470071 Nov 05 '15 at 10:36
  • I know. That's why this code does check for internet connection, and not WiFi access. – LilaQ Nov 05 '15 at 10:40
  • not working! isConnected become true when wifi is connected but has no internet! – Ahmad Vatani Nov 05 '15 at 11:22
  • 1
    This is the official example code from the [Android Documentation](http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html) about exactly your problem. – LilaQ Nov 05 '15 at 11:24
-2
Use the below class to check whether internet is available or not.

For Reference: click here to check the example

ConnectionDetector.Java

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionDetector {

private Context _context;

public ConnectionDetector(Context context){
    this._context = context;
}

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;
}
}
Prasanna
  • 195
  • 2
  • 16
  • 1
    its return true, when wifi is connected but has no internet connectivity! – Ahmad Vatani Nov 05 '15 at 11:11
  • Then Try this example.But used the above example in my project.http://androidexample.com/Check_Internet_Connectivity/index.php?view=article_discription&aid=99&aaid=121 – Prasanna Nov 06 '15 at 04:55