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 isDeviceConnectedToInternet(Context context) {

    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    return (networkInfo != null && networkInfo.isConnectedOrConnecting());
}

From source file:Main.java

/**
 * is2G// w w  w.j  a v a2  s .  c  o  m
 * @param context
 * @return boolean
 */
public static boolean is2G(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetInfo != null && (activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE
            || activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS
            || activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA)) {
        return true;
    }
    return false;
}

From source file:Main.java

public static boolean isMobile(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE;
}

From source file:Main.java

/**
 * <pre>//w ww .j a  v  a  2s.  com
 * Determine whether network is currently connected.
 * </pre>
 */
public static boolean isNetworkConnected() {
    ConnectivityManager conMan = (ConnectivityManager) getCurrentContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = conMan.getActiveNetworkInfo();
    return activeNetwork != null && activeNetwork.isConnected();
}

From source file:Main.java

/**
 * Checks for internet access//from  w  ww .  jav a  2s. co m
 *
 * @param context context
 * @return hasInternet boolean
 */
public static boolean hasInternetAccess(@NonNull Context context) {
    try {
        boolean hasInternet = false;

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if ((wifi.isAvailable() || mobile.isAvailable()) && networkInfo != null
                && networkInfo.isConnectedOrConnecting()) {

            hasInternet = true;
        }

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

From source file:Main.java

@SuppressLint("DefaultLocale")
public static boolean isWifi(Context context) {
    try {//  w w  w . j av  a2s.  c  o  m
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            NetworkInfo nInfo = cm.getActiveNetworkInfo();
            if (nInfo != null) {
                return nInfo.getTypeName().toUpperCase().equals("WIFI");
            }
        }
    } catch (Exception e) {

    }
    return false;
}

From source file:Main.java

public static boolean isNetworkConnected(Context context) {
    ConnectivityManager cManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cManager != null) {
        NetworkInfo localNetworkInfo = cManager.getActiveNetworkInfo();
        if (localNetworkInfo != null) {
            return localNetworkInfo.isConnectedOrConnecting();
        }//w ww  .java2s  .  c  o  m
    }
    return false;
}

From source file:Main.java

public static boolean isNetworkAvailable(Activity activity) {
    ConnectivityManager connectivityManager = (ConnectivityManager) activity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}

From source file:Main.java

public static int getConnectedType(Context context) {
    if (context != null) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isAvailable()) {
            return networkInfo.getType();
        }//from w  w  w .  j  av  a  2 s  .c  o m
    }
    return -1;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context mContext) {
    boolean result = false;
    try {/* w  ww . ja va 2 s  . c  o  m*/
        ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (cm != null) {
            NetworkInfo ni = cm.getActiveNetworkInfo();
            if (ni != null) {
                result = ni.isAvailable();
            }
        }
    } catch (Exception e) {
    }

    return result;

}