-1

How do I check if my app is connected to the internet?

I found this post, but it seems you can only check if connection is available. Detect whether there is an Internet connection available on Android

That post did have the correct answer, I was just too far from my wifi and would always fail the test. The problem is solved.

I would like to be able to check something like this:

if(!isConnectedToInternet())
{
     Toast.makeText(getApplicationContext(), "Please connect to the internet.", 
     Toast.LENGTH_LONG).show();
}
Community
  • 1
  • 1
J_Strauton
  • 2,270
  • 3
  • 28
  • 70

4 Answers4

1

try checking like this:

private boolean isConnectedToInternet() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

checking here:

if(!isConnectedToInternet())
{
     Toast.makeText(getApplicationContext(), "Please connect to the internet.", 
     Toast.LENGTH_LONG).show();
}
Rustam
  • 6,485
  • 1
  • 25
  • 25
1

Check this code that is work fine for me

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;
}

Check like this

boolean isInternetPresent = cd.isConnectingToInternet();
    if (isInternetPresent) {


    } else {

        Toast.makeText(getApplicationContext(),
                getResources().getString(R.string.internet_error),
                Toast.LENGTH_LONG).show();
    }
Chetan Patil
  • 542
  • 1
  • 5
  • 23
1

The answer from the link you attached actually do almost the same with what you are trying to achieve. You just need to modify few line:

private boolean isConnectedToInternet() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork= connectivityManager.getActiveNetworkInfo();
    return  activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}

You may check the documentation & guides from Android

Community
  • 1
  • 1
DnR
  • 3,487
  • 3
  • 23
  • 31
  • I was thinking that code was not working, but in fact I was too far from my wifi. So original answer was correct and so are you. Thanks! – J_Strauton Oct 03 '14 at 04:53
0

You need to check that there is an established connection

private boolean isConnected() {
ConnectivityManager cm 
      = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected() && cm.requestRouteToHost(activeNetworkInfo.getType(), yourHostAddress);
}

and then check if there is a route to the host you want (you can use Google for testing purposes). Check http://developer.android.com/reference/android/net/ConnectivityManager.html#requestRouteToHost(int, int)

Hope it helps.

Junior Buckeridge
  • 2,075
  • 2
  • 21
  • 27