Example usage for android.net NetworkInfo isConnectedOrConnecting

List of usage examples for android.net NetworkInfo isConnectedOrConnecting

Introduction

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

Prototype

@Deprecated
public boolean isConnectedOrConnecting() 

Source Link

Document

Indicates whether network connectivity exists or is in the process of being established.

Usage

From source file:Main.java

/**
 * Used to determine if there is an active data connection and what type of
 * connection it is if there is one/* w  w w. j  ava 2  s .  com*/
 * 
 * @param context The {@link Context} to use
 * @return True if there is an active data connection, false otherwise.
 *         Also, if the user has checked to only download via Wi-Fi in the
 *         settings, the mobile data and other network connections aren't
 *         returned at all
 */
public static final boolean isOnline(final Context context) {
    /*
     * This sort of handles a sudden configuration change, but I think it
     * should be dealt with in a more professional way.
     */
    if (context == null) {
        return false;
    }

    boolean state = false;

    /* Monitor network connections */
    final ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    /* Wi-Fi connection */
    final NetworkInfo wifiNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null) {
        state = wifiNetwork.isConnectedOrConnecting();
    }

    /* Mobile data connection */
    final NetworkInfo mbobileNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mbobileNetwork != null) {
        state = mbobileNetwork.isConnectedOrConnecting();
    }

    /* Other networks */
    final NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    if (activeNetwork != null) {
        state = activeNetwork.isConnectedOrConnecting();
    }

    return state;
}

From source file:net.kayateia.lifestream.Network.java

static public boolean IsActive(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork == null)
        return false;
    else/*from   w ww. j a v a 2 s . co  m*/
        return activeNetwork.isConnectedOrConnecting();
}

From source file:Main.java

/**
 * Used to determine if there is an active data connection and what type of
 * connection it is if there is one/*www .j av a2s .c o  m*/
 * 
 * @param context The {@link Context} to use
 * @return True if there is an active data connection, false otherwise.
 *         Also, if the user has checked to only download via Wi-Fi in the
 *         settings, the mobile data and other network connections aren't
 *         returned at all
 */
public static final boolean isOnline(final Context context) {
    /*
     * This sort of handles a sudden configuration change, but I think it
     * should be dealt with in a more professional way.
     */
    if (context == null) {
        return false;
    }

    boolean state = false;
    final boolean onlyOnWifi = true;//PreferenceUtils.getInstace(context).onlyOnWifi();

    /* Monitor network connections */
    final ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    /* Wi-Fi connection */
    final NetworkInfo wifiNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null) {
        state = wifiNetwork.isConnectedOrConnecting();
    }

    /* Mobile data connection */
    final NetworkInfo mbobileNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mbobileNetwork != null) {
        if (!onlyOnWifi) {
            state = mbobileNetwork.isConnectedOrConnecting();
        }
    }

    /* Other networks */
    final NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    if (activeNetwork != null) {
        if (!onlyOnWifi) {
            state = activeNetwork.isConnectedOrConnecting();
        }
    }

    return state;
}

From source file:com.photon.phresco.nativeapp.eshop.net.NetworkManager.java

public static boolean checkNetworkConnectivity(final Activity activity) {
    ConnectivityManager cm = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    } else if (netInfo != null && (netInfo.getState() == NetworkInfo.State.DISCONNECTED
            || netInfo.getState() == NetworkInfo.State.DISCONNECTING
            || netInfo.getState() == NetworkInfo.State.SUSPENDED
            || netInfo.getState() == NetworkInfo.State.UNKNOWN)) {
        return false;
    } else {//from   www .  j  av a2s  . c  om
        return false;
    }
}

From source file:Main.java

public static int hasActiveNetworkInfo(Context context) {
    int state = 0; // Assume disabled
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mActiveNetworkInfo = cm.getActiveNetworkInfo();
    if (mActiveNetworkInfo != null) {
        String typeName = mActiveNetworkInfo.getTypeName().toLowerCase();
        boolean isConnected = mActiveNetworkInfo.isConnectedOrConnecting();
        int type = mActiveNetworkInfo.getType();
        if ((isNetworkTypeMobile(type)) && (typeName.contains("mobile")) && isConnected)
            state = 2;//from www.  j  av  a  2s.  co m
        else if ((!isNetworkTypeMobile(type)) && (!typeName.contains("mobile")) && isConnected)
            state = 1;
    }
    return state;
}

From source file:Main.java

/**
 * method to check for network availability. returns true for available and
 * false for unavailable/*from ww  w. ja  va 2 s  .c  o  m*/
 */
public static boolean isConnectionAvailable(Context context) {
    ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiNetwork = conn.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo mobileNetwork = conn.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (wifiNetwork != null && wifiNetwork.isAvailable() == true
            && wifiNetwork.isConnectedOrConnecting() == true) {
        return true;
    } else if (mobileNetwork != null && mobileNetwork.isAvailable() == true
            && mobileNetwork.isConnectedOrConnecting() == true) {
        return true;
    } else
        return false;
}

From source file:net.frakbot.FWeather.util.WeatherHelper.java

/**
 * Checks if there is any network connection active (or activating).
 *
 * @param context The current {@link Context}.
 * @return Returns true if there is an active connection, false otherwise
 *//*from   w ww. j ava 2 s.c  o  m*/
public static boolean checkNetwork(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}

From source file:Main.java

/**
 * Check for wifi internet connectivity//from   w  w  w. j  a  v a 2 s  .c o  m
 *
 * @param context context
 * @return hasInternet boolean
 */
public static boolean hasWifiInternetAccess(@NonNull Context context) {
    try {
        boolean hasWifiInternet = false;
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifi != null && wifi.isAvailable() && networkInfo != null
                && networkInfo.isConnectedOrConnecting()) {

            hasWifiInternet = true;
        }

        return hasWifiInternet;
    } catch (Exception e) {
        return false;
    }
}

From source file:com.hackeruproj.android.havatzfit.general_utilities.GenUtils.java

public static boolean isNetworkEnabled(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwrok = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwrok != null && activeNetwrok.isConnectedOrConnecting();
    return isConnected;
}

From source file:com.fanfou.app.opensource.util.NetworkHelper.java

public static final boolean isConnected(final Context context) {
    final ConnectivityManager connec = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo info = connec.getActiveNetworkInfo();
    return (info != null) && info.isConnectedOrConnecting();
}