14

I tried in many ways to check Internet connection in my android emulator

  ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);  

   NetworkInfo info= conMgr.getActiveNetworkInfo();  

         if(info != null || info.isConnected()) {  
              Log.v("NetworkInfo","Connected State");  
         }  
        else{  
            Log.v("NetworkInfo","Not Connected state");  
            Log.v("Reason",info.getReason());  
        } 

even if i disable Internet connection in my system ,my code seems to display Connected state so i guessed this may work

 if ( conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED
        ||  conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED  ) {

      Log.v("Congr","Connection Present");

    }
    else if ( conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED
        ||  conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED) {

      Log.v("conMgr"," No Connection");

    } 

but the above code also displays "Connection Present" even when i disconnect Internet cables. .Please help to find a correct way to do this .I have ACCESS_NETWORK_STATE and INTERNET permission in my manifest file

Mark B
  • 183,023
  • 24
  • 297
  • 295
ganesh
  • 1,247
  • 6
  • 24
  • 46
  • 7
    Are you enabling/disabling the 3g connection in the emulator? You can do it pressing F8. – Macarse Apr 08 '10 at 12:50
  • 1
    What happens when you put the emulated device into airplane mode? – Jim Blackler Apr 08 '10 at 13:45
  • The code works when i put emulator into airplane mode .stackoverflow really rocks ,i never imaged to get an reply with mins of my post,thanks you Macarse and Jim.NetworkInfo info get null value when i put into airplane mode ,and i check "info" for null value as done above and alert the user about that .i haven't worked in real device .Is this method of checking Internet connection is right ? – ganesh Apr 08 '10 at 14:32
  • 1
    My experience with the emulator and network emulation is that it doesn't work as you might expect, and I discovered the airplane mode workaround by trial and error. As with so many platforms, getting good at Android is a lot of experimentation. – Jim Blackler Apr 09 '10 at 09:20

2 Answers2

38

1 Disconnecting internet cables is irrelevant. Use F8 to switch off/on internet in the emulator

2 In the first code this logic is wrong:

if(info != null || info.isConnected()) {  
              Log.v("NetworkInfo","Connected State");  
         } 

It should be:

if(info != null && info.isConnected()) {  
              Log.v("NetworkInfo","Connected State");  
         } 

If you use || then this is the result: a) when info is null, info.isConnected() will crash b) when info is not null, you will always show Connected State

However, even if you fix that, the code wont be completely correct (see 4)

3 In the second code, this logic is wrong:

 else if ( conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED
        ||  conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED) {

      Log.v("conMgr"," No Connection");

    } 

should be:

 else {

      Log.v("conMgr"," No Connection");

    } 

Why? Because there are more states than CONNECTED and DISCONNECTED.

However, even if you fix that, the code wont be completely correct (see 4)

4 This works on emulator and devices.

connected = (   conMgr.getActiveNetworkInfo() != null &&
            conMgr.getActiveNetworkInfo().isAvailable() &&
            conMgr.getActiveNetworkInfo().isConnected()   )

Note the use of isAvailable - without this isConnected can return TRUE when WIFI is disabled.

Hope this helps!

breakingart.com
  • 396
  • 3
  • 3
  • 2
    +1. The "1. Use F8 to switch off/on internet in the emulator", made my day! I accidentally may have pushed it and was banging my head over it. :-) – Hari Krishna Ganji Feb 14 '13 at 17:06
1

Actually, this doesn't tell you if the emulator is connected to the web via the hosting PC. If you run it on a PC and the PC's wireless is turned off it still returns true as long the emulator thinks its in online state. The only way I've been able to determine for sure whether the emulator is really online is to try to fetch a page and see if it succeeds or not.

GGizmos
  • 3,443
  • 4
  • 27
  • 72