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

5 Answers5

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,721
  • 13
  • 89
  • 124
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,522
  • 1
  • 31
  • 44
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
  • 33,936
  • 20
  • 234
  • 300
-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,851
  • 12
  • 116
  • 113
  • 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