How to check Internet access on Wi-Fi (although Wi-Fi is connected)

I used this code to check internet availability 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 I want:

Suppose that wifi is connected, but there is no Internet in wifi ... how to check data on the Internet is possible in Wi-Fi, although Wi-Fi is connected. If I use code abouve it just checks if wifi is connected or disconnected. It would be grateful if someone gives me a solution ...

+3


source to share


6 answers


The simplest is to send an HTTP request, and if your HTTP request times out, then you have no internet connection, or you can check the extra response code. The condition you mentioned happens when you are connected to Wi-Fi, but the WiFi router does not have an active connection to the ISP. Your best bet is to rely on the imo http request.

Here's some sample 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();
  }

      

+3


source


Yes VendettaDroid is right.

also here is the code to check WIFI internet and check airplane mode on / off, if you create web app then airplane mode should 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 for Wi-Fi and internet access

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

 // Internet is availble.
}

      

+1


source


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

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

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

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

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

Checking the actual internet accessibility is actually quite tricky in android. There are many restrictions for this.

The isReachable () method uses ICMP to ping the host. It is difficult to set up.

Likewise, a url call can be equally limited, depending on device, permissions, API, OS version, etc.

If you don't need it, don't do it. There is no reliable way to work across all devices.

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

It is interesting to note that you can indeed be on mobiles, but without the Internet and still pay.

+1


source


Thanks for suggesting me ...

i In fact, several solutions have been found:

I used try \ catch:

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

    e.printStackTrace();

    Connection_error();     
    }

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

      

Thanks everyone ...

0


source


You can read the current state of wifi ..

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

      

Also see this approach .

0


source


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

      

-1


source







All Articles