14

In my application, which I test on emulator, I use the following code to check the network connection (WIFI):

    public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

This method returns always true, even if I disable the wireless connection of my computer... Is this caused by the emulator or is it something else?

If this is not the right way to check network connection, how can I do that?

Sam
  • 7,252
  • 16
  • 46
  • 65
amp
  • 11,754
  • 18
  • 77
  • 133

6 Answers6

37

It's better to (1) pass in the context so that the every activity can invoke this functions, and (2) Make this function static:

 public boolean isNetworkOnline() {
    boolean status=false;
    try{
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getNetworkInfo(0);
        if (netInfo != null && netInfo.getState()==NetworkInfo.State.CONNECTED) {
            status= true;
        }else {
            netInfo = cm.getNetworkInfo(1);
            if(netInfo!=null && netInfo.getState()==NetworkInfo.State.CONNECTED)
                status= true;
        }
    }catch(Exception e){
        e.printStackTrace();  
        return false;
    }
    return status;

    }  
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Nishant
  • 32,082
  • 5
  • 39
  • 53
7

To get getActiveNetworkInfo() to work you need to add the following to the manifest.

1. uses-permission android:name="android.permission.INTERNET"
 2. uses-permission
    android:name="android.permission.ACCESS_NETWORK_STATE"

better to use netInfo.isConnected() rather than netInfo.isConnectedOrConnecting

and also try this

Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(ConnectivityManager.TYPE_MOBILE)

or

Context.getSystemService(Context.CONNECTIVITY_SERVICE).requestRouteToHost(TYPE_WIFI, int hostAddress)
Hasmukh
  • 4,632
  • 2
  • 30
  • 44
SuN
  • 1,723
  • 1
  • 13
  • 14
2

We can check internet connection connected state using below function. Google Android has updated the Android API's function to check the network state. Now in Android latest API's, getAllNetworkInfo() is deprecated. but getAllNetworks() function is only allowed for Build.VERSION.SDK_INT >= 21.I have written code for all cases.

To check the internet connection, you may call this function.

public static boolean checkInternetConnection(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
        return false;
    } else if(Build.VERSION.SDK_INT >= 21){
        Network[] info = connectivity.getAllNetworks();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i] != null && connectivity.getNetworkInfo(info[i]).isConnected()) {
                    return true;
                }
            }
        }
    } else {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
        final NetworkInfo activeNetwork = connectivity.getActiveNetworkInfo();
        if (activeNetwork != null && activeNetwork.isConnected()) {
            return true;
        }
    }

    return false;
}
houman.sanati
  • 1,054
  • 3
  • 18
  • 34
Ashish Saini
  • 2,328
  • 25
  • 21
0

In my case, I turned OFF the Wifi, but forgot about turning off the Data/Mobile network connectivity. Once I did that, the code in original post for this thread worked for me.

To recap:

  1. Go to Settings
  2. Turn Off Wifi
  3. Go to Settings->Data Usage
  4. Turn Off Mobile Data

Settings

Settings->Data Usage

Gene Bo
  • 11,284
  • 8
  • 90
  • 137
0

Use the following class, updated to the last Android version available: Android 10.

-1

This is how I put it together in a project:

Manifest file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Static Function:

public static boolean isNwConnected(Context context) {
    if (context == null) {
        return true;
    }
    ConnectivityManager connectivityManager = 
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo nwInfo = connectivityManager.getActiveNetworkInfo();
    if (nwInfo != null && nwInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}
david m lee
  • 2,547
  • 26
  • 14