Search code examples
androidandroid-contentproviderandroid-wifi

Android 2.2 vs later not handling connectivity manager the same


I have two dev devices one an android 2.2 phone and the other a Nexus7 tablet (no phone). Both have wifi. If the code below is executed on the 2.2phone it detects whether Wifi or Mobile Data is on and working. If I put the same code on the Nexus7 tablet it returns that there is no internet connection even though wifi is on. What do I have to do to make it work on both devices?

   public boolean isOnline()
   {
ConnectivityManager connMgr =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    return (networkInfo != null && networkInfo.isConnected());
   }  

Solution

  • isConnected will return true when the WIFI connection is established to a router. Are you sure you have the connections setup exactly the same on both of the devices?

    you could also try isConnectedOrConnecting() which is pretty much the same except it returns true even when a connection is not yet established to the ISP but in the process of being established.

    Also check if maybe some of the permissions have changed for jelly Bean for allowing you to check for connectivity.

    Other than that i see no reason for this method to act differently on different devices.