4

I have created a Android application using Google Maps. Now I want to check whether an internet connection is available or not. I search in Google and finally I got solution through Stackoverflow:

boolean HaveConnectedWifi = false;
boolean HaveConnectedMobile = false;
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo)
    {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                HaveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                HaveConnectedMobile = true;
    }
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />   

I have written my online Google map code if it return true and offline code if it return false.

My problem is its showing an internet connection is available even when it is not. Where have I made a mistake?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
deepa
  • 2,496
  • 1
  • 26
  • 43

2 Answers2

12

Try using NetworkInfo ni = cm.getActiveNetworkInfo(); instead of NetworkInfo ni = cm.getAllNetworkInfo();. There should only be one active data network at any one time because Android will use the best available and shut down the others to conserve battery.

Also, I tend to use ni.isConnectedOrConnecting(); instead of ni.isConnected(); because it can catch transition states better.

I also use ni.getType() == ConnectivityManager.TYPE_WIFI instead of ni.getTypeName().equalsIgnoreCase("WIFI") because it is much more efficient to compare two int values than it is to compare two strings.

The following code works for me:

boolean HaveConnectedWifi = false;
boolean HaveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if ( ni != null )
{
    if (ni.getType() == ConnectivityManager.TYPE_WIFI)
        if (ni.isConnectedOrConnecting())
            HaveConnectedWifi = true;
    if (ni.getType() == ConnectivityManager.TYPE_MOBILE)
        if (ni.isConnectedOrConnecting())
            HaveConnectedMobile = true;
}
Mark Allison
  • 21,839
  • 8
  • 47
  • 46
  • Thanks for your quick reply Sir..code works fine when its internet is connected but when i close my internet in my system its return true in ConnectivityManager.TYPE_MOBILE when run through emulator.where i have done mistake? – deepa Jun 15 '11 at 12:57
  • I'm not sure I understand your new problem. Try putting the device / emulator in to Airplane mode, and both should return false. I don't think that the emulator actually supports WIFI mode, it always assumes that the network connection is MOBILE. Id you need to test this properly you'll need to do it on a device, not the emulator. – Mark Allison Jun 15 '11 at 13:07
  • I have change my emulator in Airplane mode and run the application its showing null pointer exception in if (ni.getType() == ConnectivityManager.TYPE_WIFI). I have one more doubt when i run my google maps in mobile whether i need to get release key? if so how can i get release key? please explain in detail. i'm confused in this api key.. – deepa Jun 15 '11 at 13:10
  • Check that `ni != null` before you access the object. I've changed the code accordingly. Your google maps question is a completely different issue and would be better asked in a new question. Please don't forget to "accept" my answer. – Mark Allison Jun 15 '11 at 13:14
  • Now the exception problem is over but here i have one doubt when i put device/emulator in to Airplane Mode then it disconnect all the wireless connection and it cannot detect connection. so now the problem comes when internet connection is available its showing false and its not detecting internet connection. can you please explain in detail? – deepa Jun 16 '11 at 04:50
  • Airplane mode will shut down all of the radios in the device: Mobile, WiFi, and Bluetooth. It is not be possible to use any of them while Airplane mode is active. – Mark Allison Jun 16 '11 at 06:22
  • Then how to detect to internet connection is available or not? everything is fine when internet connection is available but when its not it return true but google map not loaded its showing only grid.. Please help me..its very urgent.. i need to release my application. – deepa Jun 16 '11 at 10:53
  • Well, you can't use maps when there's not network connection, therefore you can't use maps in Airplane mode, or when you lock network connectivity. – Mark Allison Jun 16 '11 at 11:11
  • Yes, its right when my device/emulator is not in Airplane mode that time too its not detecting internet connection correctly.. In emulator its always return true for Mobile..when it returns true it showing only grid not map when internet connection is not available. how to overcome this problem? – deepa Jun 16 '11 at 12:41
  • I'm sorry, I don't understand. Is the problem with the code above not working, or is it a problem with maps not working? – Mark Allison Jun 16 '11 at 12:44
  • its problem code always return true in emulator and map loaded when internet connection available and grid only shown when internet connection unavailable. From these i cannot able to code for both condition. my requirements is when internet connection is available i have to load map from online and its unavailable i have to load offline map(map image). since its always returning true i cannot able to load offline map.. – deepa Jun 17 '11 at 03:57
  • Maybe you need to try it on a real device to see what the behaviour is. – Mark Allison Jun 17 '11 at 06:21
  • i also having same problem if u have solved this problem then please let me know.. – Furqi Mar 20 '12 at 10:16
0
/**
 * Checking whether net connection is available or not.
 * 
 * @param nContext
 * @return true if net connection is avaible otherwise false
 */
public static boolean isNetworkAvailable(Context nContext) {
    boolean isNetAvailable = false;
    if (nContext != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) nContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (mConnectivityManager != null) {
            boolean mobileNetwork = false;
            boolean wifiNetwork = false;
            boolean mobileNetworkConnecetd = false;
            boolean wifiNetworkConnecetd = false;
            NetworkInfo mobileInfo = mConnectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            NetworkInfo wifiInfo = mConnectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (mobileInfo != null)
                mobileNetwork = mobileInfo.isAvailable();
            if (wifiInfo != null)
                wifiNetwork = wifiInfo.isAvailable();
            if (wifiNetwork == true || mobileNetwork == true) {
                if (mobileInfo != null)
                    mobileNetworkConnecetd = mobileInfo
                            .isConnectedOrConnecting();
                wifiNetworkConnecetd = wifiInfo.isConnectedOrConnecting();
            }
            isNetAvailable = (mobileNetworkConnecetd || wifiNetworkConnecetd);
        }
    }
    return isNetAvailable;

}

Also add below tag in manifest permission:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Utsav
  • 1
  • 2