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:ca.lightseed.winston.WinstonService.java

/**
 * hasConnection method checks internet connectivity status
 * @param _context a context//from  w  w  w. j av a 2  s . c  o  m
 * @return true if device has network connection
 */
public static boolean hasConnection(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:dev.application.taxivip.helpers.LocationUtils.java

/**
 * Verifico que halla al menos una conexion activa a internet
 * @param context/*from   w  w w .j a  v  a 2 s. c o  m*/
 * @return true o false
 */
public static boolean networkAvailable(Context context) {
    //Context context = getApplicationContext();
    ConnectivityManager connectMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectMgr != null) {
        NetworkInfo[] netInfo = connectMgr.getAllNetworkInfo();
        if (netInfo != null) {
            for (NetworkInfo net : netInfo) {
                if (net.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }

    return false;
}

From source file:org.gplvote.signdoc.MainActivity.java

public static boolean isInternetPresent(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) {
                    return true;
                }//from  w  ww . j  a  va2 s .  co m
    }
    return false;
}

From source file:com.dmsl.anyplace.utils.NetworkUtils.java

public static boolean haveNetworkConnection(Activity activity) {

    boolean haveWifi = false;
    boolean haveMobile = false;

    ConnectivityManager cm = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni != null && ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnectedOrConnecting())
                haveWifi = true;// w w w  .  j a  va  2s  . c o m
        if (ni != null && ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnectedOrConnecting())
                haveMobile = true;
    }
    return haveMobile || haveWifi;
}

From source file:com.rincliu.library.util.RLNetUtil.java

/**
 * @param context//  ww w. j  a v a  2 s  . com
 * @return
 */
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 {
        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:com.cloverstudio.spika.SpikaApp.java

/**
 * Checks whether this app has mobile or wireless connection
 * /*from  ww  w.j ava  2 s. com*/
 * @return
 */
public static boolean hasNetworkConnection() {
    boolean hasConnectedWifi = false;
    boolean hasConnectedMobile = false;

    final ConnectivityManager connectivityManager = (ConnectivityManager) sInstance
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo[] netInfo = connectivityManager.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                hasConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                hasConnectedMobile = true;
    }
    boolean hasNetworkConnection = hasConnectedWifi || hasConnectedMobile;
    return hasNetworkConnection;
}

From source file:org.arasthel.almeribus.utils.LoadFromWeb.java

public static boolean isConnectionEnabled(Context context) {
    boolean connected = false;
    if (context != null) {
        ConnectivityManager conManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] infos = conManager.getAllNetworkInfo();
        for (NetworkInfo netInfo : infos) {
            if (netInfo.isConnectedOrConnecting()) {
                connected = true;//from  ww w .java  2  s  . com
            }
        }
    }
    return connected;
}

From source file:com.sinpo.xnfc.nfc.Util.java

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        Log.i("NetWorkState", "Unavailabel");
        return false;
    } else {//  w  w  w. j a va2 s  .  c o  m
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    Log.i("NetWorkState", "Availabel");
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:com.normalexception.app.rx8club.html.LoginFactory.java

/**
 * Check to see if a network connection exists
 * @return   True if network connection exists
 *///from w ww.j ava2s. com
public static boolean haveNetworkConnection() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;
    boolean haveConnectedEth = false;

    Log.d(TAG, "Checking available network connections");
    ConnectivityManager cm = (ConnectivityManager) MainApplication.getAppContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        Log.d(TAG, String.format("%s is %sconnected", ni.getTypeName(), ni.isConnected() ? "" : "not "));
        if (ni.getTypeName().equalsIgnoreCase(LoginFactory.NETWORK_WIFI))
            if (ni.isConnected()) {
                Log.d(TAG, "Wifi Connection Detected");
                haveConnectedWifi = true;
            }
        if (ni.getTypeName().equalsIgnoreCase(LoginFactory.NETWORK_MOBILE))
            if (ni.isConnected()) {
                Log.d(TAG, "Mobile Connection Detected");
                haveConnectedMobile = true;
            }
        if (ni.getTypeName().equalsIgnoreCase(LoginFactory.NETWORK_ETH)
                || ni.getTypeName().equalsIgnoreCase(LoginFactory.NETWORK_ETHERNET))
            if (ni.isConnected()) {
                Log.d(TAG, "Ethernet Connection Detected");
                haveConnectedEth = true;
            }
    }
    return haveConnectedWifi || haveConnectedMobile || haveConnectedEth;
}

From source file:com.wbtech.common.CommonUtil.java

/**
 * wifi??//w w w . ja  v a 2s  . com
 * @param inContext
 * @return
 */
public static boolean isWiFiActive(Context inContext) {
    if (checkPermissions(inContext, "android.permission.ACCESS_WIFI_STATE")) {
        Context context = inContext.getApplicationContext();
        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].getTypeName().equals("WIFI") && info[i].isConnected()) {
                        return true;
                    }
                }
            }
        }
        return false;
    } else {
        if (UmsConstants.DebugMode) {
            Log.e("lost permission", "lost--->android.permission.ACCESS_WIFI_STATE");
        }

        return false;
    }
}