4

I used this code to check internet is available or not..

    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 && info[i].isAvailable())

        {
            return true;
        }
    }
    return false;

But what i want is:

Suppose wifi is connected but no internet in wifi... how to check internet data is available in wifi eventhough Wifi is connected..if i use abouve code it just checks whether wifi is connected or disconnected.. it will be thankful if some gives me a solution...

  • Btw, you could have found a answer to this by using google easily. also there are many similar questions in stackoverflow. – VendettaDroid Jan 22 '13 at 06:23

6 Answers6

3

Easiest thing is to send http request out and if your http request times out then you don't have internet connection or you can check for more response code. The condition that you mentioned occurs when you are connected to WiFi but the WiFi router does not have active connection to ISP. The best thing is to rely on http request imo.

Here is example code:

try
  {

   HttpGet request = new HttpGet(url));
   HttpParams httpParameters = new BasicHttpParams();
   // Set the timeout in milliseconds until a connection is established.
   // The default value is zero, that means the timeout is not used. 
   int timeoutConnection = 60000;
   HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
   // Set the default socket timeout (SO_TIMEOUT) 
   // in milliseconds which is the timeout for waiting for data.
   int timeoutSocket = 60000;
   HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
   // create object of DefaultHttpClient    
   DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
   request.addHeader("Content-Type", "application/json");
   HttpResponse response = httpClient.execute(request);
   // get response entity
   HttpEntity entity = response.getEntity();
   // convert entity response to string

     if (entity != null)
      {
         result = EntityUtils.toString(entity);
      }

   }
 catch (SocketException e)
  {
     return "-222" + e.toString();
  }
 catch (Exception e)
  {
     return "-333" + e.toString();
  }
VendettaDroid
  • 3,156
  • 2
  • 27
  • 41
1

Yes VendettaDroid is right.

also here is code to check the WIFI internet and check for flight mode is ON/OFF, if you are making a web application then flight mode must be handled

// Check Flight mode ON/OFF
    if (Settings.System.getInt(context.getContentResolver(),
            Settings.System.AIRPLANE_MODE_ON, 0) == 0) {

// Flight mode ON not able to access internet
}

check wifi and internet availability

ConnectivityManager cm;
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo() != null
                && cm.getActiveNetworkInfo().isAvailable()
                && cm.getActiveNetworkInfo().isConnected()) {

 // Internet is availble.
}
Qadir Hussain
  • 8,708
  • 11
  • 87
  • 123
1

Just worked through this for the latest title "Mobile Free" on Play Store https://play.google.com/store/apps/details?id=com.mobilefree

NetworkInfo.isAvailable() just tells you if there are physical networks available to connect to. NetworkInfo.isConnected() just tells you that you are connected to a physical network.

A physical network can be WIFI, MOBILE, ETHER, etc ... the transport layer.

Just because you are connected to a network does not mean that network has internet (an application layer).

Networks can have all sorts of application protocols. TCP, DNS and HTTP are not the only ones.

Checking for actual internet availability is actually quite difficult in android. There are many restrictions to overcome to do that.

The isReachable() method uses ICMP to ping a host. That can be hard to set up.

Similarly, calling a url can be equally restricted, depending on the device, permissions, API, OS version, etc.

If you don't have to do it, dont. There is no robust way that will work on all devices.

This was not a core use case for "Mobile Free", which just manages mobile billing (on/off).

Interesting to note you can actually be on mobile, but with no internet and still be billable.

Dominic Cerisano
  • 3,208
  • 1
  • 27
  • 42
0

Thanks a lot for suggesting me...

i Actually found some solutios:

i used try\catch:

    try{
    //calling url
    }
    catch(UnknownHostException e){

    e.printStackTrace();

    Connection_error();     
    }

   catch (ConnectException e) {
    e.printStackTrace();
    Connection_error();
            }

Thanks to All...

0

You can read the wifi current state..

SupplicantState supState; 
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
supState = wifiInfo.getSupplicantState();

Also have a look at this approach.

Community
  • 1
  • 1
Zar E Ahmer
  • 32,807
  • 18
  • 222
  • 281
-1

try below code:-

public static boolean netConnect(Context ctx) {

    ConnectivityManager cm;
    NetworkInfo info = null;
    try {
        cm = (ConnectivityManager) ctx
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        info = cm.getActiveNetworkInfo();
    } catch (Exception e) {

        e.printStackTrace();
    }
    if (info != null) {
        return true;

    } else {
        return false;
    }
}
duggu
  • 37,191
  • 12
  • 114
  • 111
  • HCD: Actually Wifi is connected...So this code will return true. if no internet is connected to wifi but wifi is connected to device.device juz check wifi is connected or not.. – Mohamed Hisham Ibn Hanifa Jan 22 '13 at 07:19