0

I have made use of a BroadcastReceiver for IntentFilter named android.net.conn.CONNECTIVITY_CHANGE but while checking through the emulator, I found out that even if there is no true connectivity to the internet, if the mobile data is switched on in the emulator it shows that the connection is alive.

CASE | MO_DATA | MO_WIFI | PC_INTER | RESP
---- | ------- | ------- | -------- | ----
1    | ON      | ON      | ON       | ON
2    | ON      | OFF     | ON       | ON
3    | OFF     | ON      | ON       | ON
4    | OFF     | OFF     | ON       | OFF
5    | ON      | ON      | OFF      | ON
6    | ON      | OFF     | OFF      | ON
7    | OFF     | ON      | OFF      | ON
8    | OFF     | OFF     | OFF      | OFF

The response shows ON even when there is no internet connectivity in the PC in cases 5, 6 and 7 above only because either of mobile data/wifi or both are switched ON and the actual status of non-availability of internet is not reflected.

Is there any way to solve this issue?

2 Answers2

2

One way to check whether you have working internet connection is to make a request to Google.com and If you get the response 200 then you have a working net connection otherwise not.

If you will use CONNECTIVITY MANAGER then there is no way to find out if the connected network has working internet or not as sometime it happens like we are connected with WiFi but it doesn't work. Hope this will help you..

Deepesh
  • 523
  • 4
  • 11
  • What you said is a very plausible solution but how to check for the response code with a HTTP connection? Is there any way to do that? It would be very helpful to get such a usable library. – Sritam Saha Jan 29 '17 at 13:33
  • have a look at this link http://stackoverflow.com/questions/6467848/how-to-get-http-response-code-for-a-url-in-java – Deepesh Jan 29 '17 at 13:40
0

BroadcastReceiver with intent filter android.net.conn.CONNECTIVITY_CHANGE gets fired when there is change in the network. Either Network Connected - Disconnected or Vice versa

Ajay Shrestha
  • 2,433
  • 1
  • 21
  • 25