Example usage for android.net ConnectivityManager getActiveNetworkInfo

List of usage examples for android.net ConnectivityManager getActiveNetworkInfo

Introduction

In this page you can find the example usage for android.net ConnectivityManager getActiveNetworkInfo.

Prototype

@Deprecated
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
@Nullable
public NetworkInfo getActiveNetworkInfo() 

Source Link

Document

Returns details about the currently active default data network.

Usage

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null)
        return false;
    NetworkInfo info = cm.getActiveNetworkInfo();
    if (info != null && info.isAvailable())
        return true;
    return false;
}

From source file:Main.java

public static boolean isAccessNetwork(Context context) {
    boolean result;
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netinfo = connManager.getActiveNetworkInfo();
    if (netinfo != null && netinfo.isAvailable()) {
        result = true;//from w ww.j av  a2s. c o  m
    } else {
        result = false;
    }
    return result;
}

From source file:Main.java

public static boolean is4G(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetInfo != null && activeNetInfo.isConnectedOrConnecting()) {
        if (activeNetInfo.getType() == TelephonyManager.NETWORK_TYPE_LTE) {
            return true;
        }//  w  w  w . ja  v  a2  s  . com
    }
    return false;
}

From source file:Main.java

/**
 * Returns true if network is available/*from   ww w  .  ja  va2 s  . c  o  m*/
 */
static boolean isNetworkAvailable(Context context) {
    // Copied from
    // https://udacity-github-sync-content.s3.amazonaws.com/_imgs/752/1436612063/Screen_Shot_2015-07-11_at_12.53.52.png
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

From source file:Main.java

/**
 * Check for wifi internet connectivity/*from   w  w  w. ja  va  2 s  .co  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:Main.java

/**
 * Returns network availability status./*  ww  w .  j a v a 2s . c o  m*/
 * @param context - Application context.
 * @return - Network availability status.
 */
public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectivityManager.getActiveNetworkInfo();
    return info != null && info.isConnected();
}

From source file:Main.java

public static boolean isWifi(Context mContext) {
    ConnectivityManager connectivityManager = (ConnectivityManager) mContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI;
}

From source file:Main.java

public static boolean checkNetworkConnectivity(Context context) {
    // get the system ConnectivityManager
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null && (networkInfo.isAvailable() || networkInfo.isConnectedOrConnecting())) {
        if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
            return true;
        }// w  w w.  jav  a  2s .  c  o  m
        // record the fact that there is live connection
    }
    return false;

}

From source file:Main.java

/**
 * Returns network availability status./*from  ww  w  .j a v  a 2s  . co m*/
 * @param context - Application context.
 * @return - Network availability status.
 */
public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectivityManager.getActiveNetworkInfo();
    return (info != null && info.isConnected());
}

From source file:Main.java

/**
 * Method to check weather the network is available in the device
 * //from   w w  w. j  a v  a 2  s.  c o m
 * @param context
 * @return
 */
private static boolean isConnectionAvailable(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}