Example usage for android.net ConnectivityManager getAllNetworkInfo

List of usage examples for android.net ConnectivityManager getAllNetworkInfo

Introduction

In this page you can find the example usage for android.net ConnectivityManager getAllNetworkInfo.

Prototype

@Deprecated
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
@NonNull
public NetworkInfo[] getAllNetworkInfo() 

Source Link

Document

Returns connection status information about all network types supported by the device.

Usage

From source file:Main.java

/**
 * Function to check if network is available
 * */// w  w w. ja  v a  2  s  . c  om
public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager cn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cn == null) {
        return false;
    }/*w  w  w.  j  av  a2  s.c o  m*/
    NetworkInfo[] netinfo = cn.getAllNetworkInfo();
    if (netinfo == null) {
        return false;
    }
    for (NetworkInfo net : netinfo) {
        if (net.isConnected()) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * @author Cheb//from w w  w .j  a  va2 s .co m
 * @return Internet connectivity status
 */
public static boolean isConnectingToInternet(Activity activity) {
    ConnectivityManager connectivity = (ConnectivityManager) activity
            .getSystemService(activity.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    boolean netstate = false;
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {

        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {

                if (info[i].getState() == NetworkInfo.State.CONNECTED) {

                    netstate = true;/*from ww  w .  ja v  a  2s.  c o m*/
                    break;
                }
            }
        }
    }
    return netstate;
}

From source file:Main.java

public static boolean isWifiActive(Context icontext) {
    Context context = icontext.getApplicationContext();
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] info;/*from w w w .  j  a va2s. c om*/
    if (connectivity != null) {
        info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getTypeName().equals("WIFI") && info[i].isConnected()) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:Main.java

public static boolean checkNetState(Context context) {
    boolean netstate = false;
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    netstate = true;//from  ww  w.  jav  a 2  s  . c om
                    break;
                }
            }
        }
    }
    return netstate;
}

From source file:Main.java

public static final boolean hasConnectivity(final Context context, final int... networkTypes) {
    final ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (networkTypes.length == 0)
        for (final NetworkInfo info : connectivity.getAllNetworkInfo())
            if (info.isAvailable() || info.isConnected() || info.isConnectedOrConnecting())
                return true;
    for (final int networkType : networkTypes)
        if (ConnectivityManager.isNetworkTypeValid(networkType)) {
            final NetworkInfo info = connectivity.getNetworkInfo(networkType);
            if (info.isAvailable() || info.isConnected() || info.isConnectedOrConnecting())
                return true;
        }//from   w ww  .ja  va 2 s .co  m
    return false;
}

From source file:Main.java

public static int isNetworkAvailable(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (NetworkInfo anInfo : info) {
                if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
                    if (anInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                        return 1;
                    } else if (anInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                        String extraInfo = anInfo.getExtraInfo();
                        if ("cmwap".equalsIgnoreCase(extraInfo) || "cmwap:gsm".equalsIgnoreCase(extraInfo)) {
                            return 2;
                        }/*w ww .  j av a2s.  com*/
                        return 3;
                    }
                }
            }
        }
    }
    return 0;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    Object connectivityService = context.getSystemService(Context.CONNECTIVITY_SERVICE);
    ConnectivityManager connectivityManager = (ConnectivityManager) connectivityService;
    if (connectivityManager == null) {
        return false;
    } else {//from   w  w w  .  java 2s .  co m
        NetworkInfo[] networkInfos = connectivityManager.getAllNetworkInfo();
        if (networkInfos != null && networkInfos.length > 0) {
            for (NetworkInfo networkInfo : networkInfos) {
                if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:Main.java

public static boolean isWifiActive(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] info;/*w  w  w. ja v a  2 s .c om*/
    if (connectivity != null) {
        info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getType() == ConnectivityManager.TYPE_WIFI && info[i].isConnected()) {
                    return true;
                }
            }
        }
    }
    return false;
}