Example usage for android.net ConnectivityManager TYPE_MOBILE

List of usage examples for android.net ConnectivityManager TYPE_MOBILE

Introduction

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

Prototype

int TYPE_MOBILE

To view the source code for android.net ConnectivityManager TYPE_MOBILE.

Click Source Link

Document

A Mobile data connection.

Usage

From source file:Main.java

/**
 * Checks for internet access/*from  w ww  . jav a 2 s  . c  om*/
 *
 * @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

/**
 * /*w w  w  .j a  v  a2s. c om*/
 * @param context
 * @return
 */
public static boolean isNetworkRoaming(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        Log.w(LOG_TAG, "couldn't get connectivity manager");
    } else {
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (info != null && info.getType() == ConnectivityManager.TYPE_MOBILE) {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (tm != null && tm.isNetworkRoaming()) {
                Log.d(LOG_TAG, "network is roaming");
                return true;
            } else {
                Log.d(LOG_TAG, "network is not roaming");
            }
        } else {
            Log.d(LOG_TAG, "not using mobile network");
        }
    }
    return false;
}

From source file:Main.java

/**
 * Checks if Network connectivity is available to download OpenCellID data
 * Requires:        android:name="android.permission.ACCESS_NETWORK_STATE"
 *//*from   ww  w  .j  a  v a2s.c  o  m*/
public static Boolean isNetAvailable(Context context) {

    try {
        ConnectivityManager cM = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiInfo = cM.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobileInfo = cM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (wifiInfo != null && mobileInfo != null) {
            return wifiInfo.isConnected() || mobileInfo.isConnected();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static int getNetWorkType(Context context) {
    int netType = NETWORK_NO;
    NetworkInfo info = getActiveNetworkInfo(context);
    if (info != null && info.isAvailable()) {

        if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            netType = NETWORK_WIFI;/*www. j a v  a2  s .c om*/
        } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            switch (info.getSubtype()) {

            case NETWORK_TYPE_GSM:
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                netType = NETWORK_2G;
                break;

            case NETWORK_TYPE_TD_SCDMA:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                netType = NETWORK_3G;
                break;

            case NETWORK_TYPE_IWLAN:
            case TelephonyManager.NETWORK_TYPE_LTE:
                netType = NETWORK_4G;
                break;
            default:

                String subtypeName = info.getSubtypeName();
                if (subtypeName.equalsIgnoreCase("TD-SCDMA") || subtypeName.equalsIgnoreCase("WCDMA")
                        || subtypeName.equalsIgnoreCase("CDMA2000")) {
                    netType = NETWORK_3G;
                } else {
                    netType = NETWORK_UNKNOWN;
                }
                break;
            }
        } else {
            netType = NETWORK_UNKNOWN;
        }
    }
    return netType;
}

From source file:Main.java

static public synchronized boolean checkConnectivity(Context context) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    // mMobile = prefs.getBoolean(ENUMPrefs.ENUM_PREF_MOBILE, false);
    mOnline = false;/*from   ww w  .  j  a v a2  s.  co m*/

    ConnectivityManager mCmgr = (ConnectivityManager) context.getSystemService(Service.CONNECTIVITY_SERVICE);

    NetworkInfo ni = mCmgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (ni != null && ni.isConnected()) { /* wifi is on */
        mOnline = true;
    } else {
        if (mMobile) {
            /* if mobile is active and EDGE or better, we're good */
            ni = mCmgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (ni != null && ni.isConnected()) {
                mOnline = (ni.getSubtype() >= TelephonyManager.NETWORK_TYPE_EDGE);
            }
        }
    }
    return mOnline;
}

From source file:Main.java

/**
 * Checks for network connectivity via WiFi or Mobile.
 *
 * @param context Context of application
 * @return A concatenated string with two fields separated by a colon.  The first field is Wifi
 * and second field is Mobile.  If wifi is up, there will be a 'w', otherwise no character.
 * If mobile is up, there will be a 'm', otherwise no character.  If neither is up, then the
 * string will only be the colon.//from   w ww .  ja v a  2  s. c  o  m
 */
public static String getNetworkStatus(Context context) {

    String result = "";

    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
    boolean wifiConnected;
    boolean mobileConnected;
    if (activeInfo != null && activeInfo.isConnected()) {
        wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
        mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
    } else {
        wifiConnected = false;
        mobileConnected = false;
    }

    if (wifiConnected)
        result += "w";
    result += ":";
    if (mobileConnected)
        result += "m";
    return result;
}

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  a va  2 s  . c om*/
 * 
 * @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 = false;// 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:Main.java

public static final boolean isNetworkAvailable(Context paramContext) {
    ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext
            .getSystemService(Activity.CONNECTIVITY_SERVICE);
    final NetworkInfo localNetworkInfo1 = localConnectivityManager
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    final NetworkInfo localNetworkInfo2 = localConnectivityManager
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    return (localNetworkInfo1.isConnected() && localNetworkInfo2.isConnected());
}

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;
            }/*from   w w w  .ja  v  a 2  s  .c o  m*/
        case ConnectivityManager.TYPE_WIFI:

            return NETWORK_CLASS_WIFI;
        }
    }
    return NETWORK_CLASS_UNKNOWN;
}

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.  ja v  a 2  s .co  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;
}