Example usage for android.net NetworkInfo isConnected

List of usage examples for android.net NetworkInfo isConnected

Introduction

In this page you can find the example usage for android.net NetworkInfo isConnected.

Prototype

@Deprecated
public boolean isConnected() 

Source Link

Document

Indicates whether network connectivity exists and it is possible to establish connections and pass data.

Usage

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    try {// w  w  w .j ava2s  .c  om
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return false;
}

From source file:Main.java

public static boolean isGPRSConnected(Context context) {
    ConnectivityManager cmg = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean result = true;
    NetworkInfo active = cmg.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (active == null || !active.isConnected()) {
        result = false;/*from  w  w  w .ja v  a  2  s.c  om*/
    }
    return result;
}

From source file:Main.java

/**
 * Checks whether the device is connected to a network
 *///  w ww .ja v a2 s .  c  o m
public static boolean hasInternet(Activity 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"))
            if (ni.isConnected())
                hasConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("mobile"))
            if (ni.isConnected())
                hasConnectedMobile = true;
    }
    return hasConnectedWifi || hasConnectedMobile;
}

From source file:Main.java

private static boolean isWifiConnected(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (info != null && info.isConnected()) {
        return true;
    }//from w  ww .  j  a  v  a2s. c  o  m

    return false;

}

From source file:Main.java

public static boolean isOnline(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
}

From source file:Main.java

private static boolean isMobileConnected(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (info != null && info.isConnected()) {
        return true;
    }//from w w w.ja  v a  2  s  .c  om

    return false;
}

From source file:Main.java

public static boolean hasConnection(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null && wifiNetwork.isConnected()) {
        return true;
    }/*from  www. ja va2  s  .c o m*/

    NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mobileNetwork != null && mobileNetwork.isConnected()) {
        return true;
    }

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected()) {
        return true;
    }

    return false;
}

From source file:Main.java

/**
 * Detects the availability of a working Wifi network connection to open a
 * http/https connection./*from   w  w w  .j  a  v a 2s. c om*/
 *
 * @param context The context of the activity who is trying to check for the
 *                status of the network.
 * @return true if Wifi network is available, false otherwise.
 */
public static boolean isWiFiAvailable(Context context) {
    if (context == null)
        return false;

    ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiNetwork = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null && wifiNetwork.isConnected()) {
        return true;
    } else {
        return false;
    }

}

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
 */// ww w . j  av  a 2 s  .  c o  m
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

/**
 *
 * Check whether network is connected or not
 *
 * @param context Context of application
 * @return true if connected false otherwise
 *//*from ww  w.j  av a  2 s.c  o m*/
public static boolean isNetworkAvailable(Context context) {

    boolean connected = false;
    ConnectivityManager connectivityManager = null;
    connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
    if (netInfo != null) {
        connected = netInfo.isConnected();
    }
    return connected;
}