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 isActiveNetworkWifi(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null) {
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            return true;
        }/*from   ww w.j av  a  2s . c  o  m*/
    }
    return false;
}

From source file:Main.java

public static boolean isActiveNetworkMobile(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null) {
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
            return true;
        }/* ww  w .  ja  v  a2  s. com*/
    }
    return false;
}

From source file:eu.vranckaert.worktime.utils.network.NetworkUtil.java

/**
 * Checks if the device is connected with internet or not.
 * @param ctx The app-context./*from  www  .j  a  va  2  s .com*/
 * @return {@link Boolean#TRUE} if the device is connected or connecting, {@link Boolean#FALSE} if no connection is
 * available.
 */
public static boolean isOnline(Context ctx) {
    ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        Log.d(LOG_TAG, "Device is online");
        return true;
    }
    Log.d(LOG_TAG, "Device is not online");
    return false;
}

From source file:Main.java

public static boolean isNetworkConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null) {
            return mNetworkInfo.isAvailable();
        }/*ww  w.java  2 s .c  o m*/
    }
    return false;
}

From source file:Main.java

public static boolean checkNet(Context context) {
    try {/*from  www. java 2s .c o m*/
        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) {
        return false;
    }
    return false;
}

From source file:com.qihoo.permmgr.util.d.java

public static boolean a(Context paramContext) {
    try {//from   www .  ja v  a 2 s. c  o  m
        ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext
                .getSystemService("connectivity");
        if (localConnectivityManager.getActiveNetworkInfo() != null) {
            boolean bool = localConnectivityManager.getActiveNetworkInfo().isAvailable();
            return bool;
        }
    } catch (Exception localException) {
        localException.printStackTrace();
        return true;
    }
    return false;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    try {/* ww w  . j  a  va 2 s  . 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) {
        return false;
    }
    return false;
}

From source file:Main.java

public static int getConnectedType(Context context) {
    ConnectivityManager cManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo nInfo = cManager.getActiveNetworkInfo();
    if (nInfo != null && nInfo.isAvailable()) {
        int type = nInfo.getType();
        int subType = nInfo.getSubtype();
        switch (type) {
        case ConnectivityManager.TYPE_MOBILE:
            switch (subType) {
            case 1://TelephonyManager.NETWORK_TYPE_GPRS:
            case 2://TelephonyManager.NETWORK_TYPE_EDGE:
            case 4://TelephonyManager.NETWORK_TYPE_CDMA:
            case 7://TelephonyManager.NETWORK_TYPE_1xRTT:
            case 11://TelephonyManager.NETWORK_TYPE_IDEN:
                return NETWORK_CLASS_2_G;
            case 3://TelephonyManager.NETWORK_TYPE_UMTS:
            case 5://TelephonyManager.NETWORK_TYPE_EVDO_0:
            case 6://TelephonyManager.NETWORK_TYPE_EVDO_A:
            case 8://TelephonyManager.NETWORK_TYPE_HSDPA:
            case 9://TelephonyManager.NETWORK_TYPE_HSUPA:
            case 10://TelephonyManager.NETWORK_TYPE_HSPA:
            case 12://TelephonyManager.NETWORK_TYPE_EVDO_B:
            case 14://TelephonyManager.NETWORK_TYPE_EHRPD:
            case 15://TelephonyManager.NETWORK_TYPE_HSPAP:
                return NETWORK_CLASS_3_G;
            case 13://TelephonyManager.NETWORK_TYPE_LTE:
                return NETWORK_CLASS_4_G;
            default:
                return NETWORK_CLASS_UNKNOWN;
            }/*  w  ww.  j  a  v a2 s.com*/
        case ConnectivityManager.TYPE_WIFI:

            return NETWORK_CLASS_WIFI;
        }
    }
    return NETWORK_CLASS_UNKNOWN;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    if (context == null) {
        Log.e("isNetworkAvailable", " Context is null");
        return false;
    }/*from  w  w  w.  j av  a  2  s  .  c  o m*/
    try {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            return info.isAvailable();
        }
    } catch (Exception e) {
        return false;
    }
    return false;
}

From source file:Main.java

public static boolean isWapNet(Context context) {
    ConnectivityManager conManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = conManager.getActiveNetworkInfo();
    if (info != null && info.isAvailable()) {
        if (info.getType() == 1) {
            return false;
        } else {/*from  ww  w.j a v  a 2 s . com*/
            String currentAPN = info.getExtraInfo();
            return !TextUtils.isEmpty(currentAPN) && (currentAPN.equals("cmwap") || currentAPN.equals("uniwap")
                    || currentAPN.equals("3gwap"));
        }
    } else {
        return false;
    }
}