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

/**
 * Metodo para saber si se tiene conexion a internet
 * por medio de datos o wifi/*from   w  w w. j av  a2s. c  om*/
 * @param context
 * @return
 */
public static boolean hasInternet(Context a) {
    boolean hasConnectedWifi = false;
    boolean hasConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) a.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase(WIFI_TYPE_NAME))
            if (ni.isConnected())
                hasConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase(MOBILE_TYPE_NAME))
            if (ni.isConnected())
                hasConnectedMobile = true;
    }
    return hasConnectedWifi || hasConnectedMobile;
}

From source file:Main.java

public static boolean isNetworkConnected(Context context) {
    ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] info = mgr.getAllNetworkInfo();
    if (info != null) {
        for (int i = 0; i < info.length; i++) {
            if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }/*from   ww  w .  j a  v  a 2s  .c o  m*/
        }
    }
    return false;
}

From source file:Main.java

public static boolean HaveNetworkConnection(Context ctx) {

    boolean HaveConnectedWifi = false;
    boolean HaveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI")) {
            if (ni.isConnected()) {
                HaveConnectedWifi = true;
            }/*from www.j  a  va  2s . c  o  m*/
        }
        if (ni.getTypeName().equalsIgnoreCase("MOBILE")) {
            if (ni.isConnected()) {
                HaveConnectedMobile = true;
            }
        }
    }
    return HaveConnectedWifi || HaveConnectedMobile;
}

From source file:Main.java

public static boolean isNetworkAvailable(ConnectivityManager cm) {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    NetworkInfo[] mNetInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : mNetInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }/*from  ww w . j av  a  2  s .  co m*/
    return haveConnectedWifi || haveConnectedMobile;
}

From source file:Main.java

/**
 * Checks for network connectivity either via wifi or cellular.
 * @param context The context of the activity doing the checking
 * @return A Boolean. True if they have connection, false if they do not
 *//*from www  .j  a v  a2s.c om*/
public static boolean haveNetworkConnection(Context context) {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;
    if (context == null) {
        return false;
    }
    ConnectivityManager cm = (ConnectivityManager) context.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;
    }
    return haveConnectedWifi || haveConnectedMobile;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context ctx) {
    ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo[] netInfo = cm.getAllNetworkInfo();

    for (NetworkInfo ni : netInfo) {
        String name = ni.getTypeName();

        if (name.equalsIgnoreCase("ETH"))
            if (ni.isConnected())
                return true;

        if (name.equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                return true;

        if (name.equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                return true;
    }//  w  ww  .j a va  2s  .  c o m

    return false;
}

From source file:Main.java

public static boolean isConnectingToInternet(Context ctx) {
    ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm != null) {
        NetworkInfo[] info = cm.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }/* ww  w. j a  v a  2s.  c  o m*/
            }
        }
    }
    return false;
}

From source file:Main.java

public static boolean checkInternetConnection(Context context) {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    try {/*  w ww  . j  av  a 2s .co  m*/
        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;
        }
    } catch (Exception e) {
        e.getStackTrace();
    }
    return haveConnectedWifi || haveConnectedMobile;
}

From source file:Main.java

public static boolean isNetworkAvaliable(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager != null) {
        NetworkInfo[] info = manager.getAllNetworkInfo();
        for (int i = 0; i < info.length; i++) {
            if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }//from   w  w w.  jav a2  s.  c  o  m
        }
    }
    return false;
}

From source file:Main.java

public static boolean isOnline(final Context context) {
    final ConnectivityManager conMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    final NetworkInfo[] networkInfos = conMgr.getAllNetworkInfo();
    for (final NetworkInfo networkInfo : networkInfos) {
        if ((networkInfo.getState() == NetworkInfo.State.CONNECTED)
                || (networkInfo.getState() == NetworkInfo.State.CONNECTING)) {
            return true;
        }/* w  ww .j  a v a 2  s. c  o  m*/
    }
    return false;
}