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

/**
 * Check if there connection/*ww  w .j a  v  a 2s  .c  o m*/
 * @param context
 * @return
 */
public static boolean thereConnection(Context context) {

    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager == null) {
        return false;
    }

    NetworkInfo[] info = connectivityManager.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 int isNetworkAvailable(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        return 0;
    } else {//from   w  w w . j a  v  a  2s  . c  o  m
        NetworkInfo[] infos = connectivity.getAllNetworkInfo();
        if (infos != null) {
            for (NetworkInfo info : infos) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    if (info.getType() == ConnectivityManager.TYPE_WIFI) {
                        return 1;
                    } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
                        String extraInfo = info.getExtraInfo();
                        if ("cmwap".equalsIgnoreCase(extraInfo) || "cmwap:gsm".equalsIgnoreCase(extraInfo))
                            return 2;
                        return 3;
                    }
                }
            }
        }
    }
    return 0;
}

From source file:Main.java

public static boolean checkNetworkAvailable(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        return false;
    } else {/*from www. j a  v  a  2s .  c  om*/
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    NetworkInfo netWorkInfo = info[i];
                    if (netWorkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                        return true;
                    } else if (netWorkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                        return true;
                    }
                }
            }
        }
    }

    return false;

}

From source file:it.unicaradio.android.utils.NetworkUtils.java

public static boolean isConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    for (NetworkInfo info : connectivityManager.getAllNetworkInfo()) {
        if (info.isConnected()) {
            return true;
        }//ww  w . j  a  v  a 2  s  .  c  om
    }

    return false;
}

From source file:com.emsitel.emsivoz.MainActivity.java

public static boolean verificaConexion(Context ctx) {
    boolean bConectado = false;
    ConnectivityManager connec = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    // No slo wifi, tambin GPRS
    NetworkInfo[] redes = connec.getAllNetworkInfo();
    // este bucle debera no ser tan apa
    for (int i = 0; i < 2; i++) {
        // Tenemos conexin? ponemos a true
        if (redes[i].getState() == NetworkInfo.State.CONNECTED) {
            bConectado = true;//from w  w w .j  a v  a 2s  . c om
        }
    }
    return bConectado;
}

From source file:Main.java

public static boolean isConnected(Context mContext) {
    ConnectivityManager connectivityManager = (ConnectivityManager) mContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivityManager == null) {
        return false;
    }/*from   w ww  .j a va 2  s .  c o m*/

    NetworkInfo[] networkInfoList = connectivityManager.getAllNetworkInfo();

    if (networkInfoList != null) {

        for (NetworkInfo nwInfo : networkInfoList) {

            if (nwInfo.isConnected()) {
                return true;
            }
        }
    }
    return false;
}

From source file:Main.java

public static boolean isNetworkAvailable(Activity activity) {
    Context context = activity.getApplicationContext();
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivityManager == null) {
        return false;
    } else {/*from w w w  . ja  v a  2s .c o  m*/
        NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

        if (networkInfo != null && networkInfo.length > 0) {
            for (int i = 0; i < networkInfo.length; i++) {

                if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        Log.w(LOG_TAG, "couldn't get connectivity manager");
    } else {/* ww  w  . j a va  2  s. c  o  m*/
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].isAvailable()) {
                    Log.d(LOG_TAG, "network is available");
                    return true;
                }
            }
        }
    }
    Log.d(LOG_TAG, "network is not available");
    return false;
}

From source file:it.sasabz.android.sasabus.classes.hafas.XMLRequest.java

/**
 * this method checks if a network-connection is active or not
 * @return boolean if the network is reachable or not
 *//*www .j  a  v  a 2 s  .  co m*/
public static boolean haveNetworkConnection() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) (SASAbus.getContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE));
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        //testing WIFI connection
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        //testing GPRS/EDGE/UMTS/HDSPA/HUSPA/LTE connection
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;
}

From source file:Main.java

/**
 * Check if the device has internet access
 * //  ww  w . j a  v a 2  s .c  om
 * @param context
 * @return
 */
public static boolean isDeviceConnected(Context context) {

    // get a ConnectivityManager from the context
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    // double check if we got a ConnectivityManager at all
    if (connectivity == null) {

        return false;
    } 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;
                }
            }
        }
    }
    return false;
}