2

I'm looking for best technique to verify internet over WiFi or Cell. Let's say you are connected to some AP but you don't now if internet exists.

Today I have 2 options by using HttpURLConnection:

  • Download file from cdn server
  • to send request to trust web page like google.com and get response 200.

Any other ways?

All suggestions would be appreciated.

Maxim Shoustin
  • 76,444
  • 28
  • 192
  • 219
  • any reason that your methods are insufficient? – mfrankli Oct 24 '12 at 14:43
  • 1
    Check this thread here at stack overflow! http://stackoverflow.com/questions/6493517/android-detect-if-device-has-internet-connection – just_user Oct 24 '12 at 14:45
  • Well, this question derived from other one , take a look on [link](http://stackoverflow.com/questions/12881142/android-checking-for-internet-if-network-is-available/12881268#12881268). Someone told me _this is wrong approach. you testing if certain site can be accessed, not if network is up. _ – Maxim Shoustin Oct 24 '12 at 14:47
  • I got -1 on my response where I described above mentioned problem. So I don't know who is right there. Someone told me to listen to other applications like Gmail sync ... . – Maxim Shoustin Oct 24 '12 at 14:49
  • Anyways thanks to @just_user on link. – Maxim Shoustin Oct 24 '12 at 14:51
  • 1
    "but you don't now if internet exists" - last time I checked it was here :) – Marcin Orlowski Oct 24 '12 at 14:56

3 Answers3

1

See the Telephony Manager and it's getDataSate() method it has four state

DATA_DISCONNECTED
DATA_CONNECTING
DATA_CONNECTED
DATA_SUSPENDED

You can get the instance of telephony manager --

 TelephonyManager tm = (TelephonyManager)getSystemService(Telephony_Service); 
Sunny
  • 13,174
  • 14
  • 76
  • 125
1

On some level, 'ability to access internet endpoints' is equivalent to 'having internet access'. You could consider some algorithm:

for (Endpoint site : theEntireInternet) {
    if (can connect to site) return true;
}
return false;

which will conclusively establish connectivity. In other words, being able to connect to any one site is sufficient positive proof, but theoretically you would need to enumerate every site to prove non-connectivity. In practice, checking a few major sites is obviously sufficient. Otherwise (without some sort of meta-information about networks, ISPs, etc; which is unavailable) there's no way to conclusively demonstrate "internet" connectivity other than... connecting to the internet.

Of course as you comment, checking various internet-based applications can't hurt either; it's just a different form of an equivalent technique.

mfrankli
  • 2,733
  • 16
  • 21
  • You are right ,to run in loop on several sites is better approach than only one. It's increases probability to get true on WiFi Internet – Maxim Shoustin Oct 24 '12 at 15:10
  • right - the idea here is that even though your theoretically can't be SURE that there's no internet just because you can't connect to google.com, if you try a dozen major sites and they're all unavailable, you can probably feel pretty confident. it's a somewhat trivial distinction, in any case – mfrankli Oct 24 '12 at 15:13
0

Here it is:

to see the state of the data connection on the cell network

and for the WIFI, I would use this public class WifiInfo. From there you can use getIpAddress() or getDetailedStateOf(...) Hope it's helpful.

Sylvain Huard
  • 1,248
  • 4
  • 18
  • 28
  • I don't know if you receive IP on connecting, you could be sure that you have Internet. I used `getConnectionInfo().getSupplicantState()` to fix Supplicant on `DORMANT`, `DISCONNECTED` or `SCANNING` and make WiFi manager to reconnect – Maxim Shoustin Oct 24 '12 at 15:16
  • It's a good point. I had a similar problem on a GSM data connection. Even though I had an IP address, the status of the data connection was reported as "limited connectivity" where you can place an emergency call but you don't have data link even though I could see an IP. That actually is a problem in Gingerbread because if the GSM signal level comes up again without having completely lost it, it will take hours before getting a valid data link. I fixed it by killing DHCP service when "limited service" appears. – Sylvain Huard Oct 24 '12 at 17:38