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

public static boolean isMobileDataEnable(Context context) throws Exception {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean isMobileDataEnable = false;
    isMobileDataEnable = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
            .isConnectedOrConnecting();//from ww w .  jav a  2  s  .  c om
    return isMobileDataEnable;
}

From source file:Main.java

public static boolean is3gConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm != null) {
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
            return true;
        }/*  w  w  w.j  a v a 2s  .c  om*/
    }
    return false;
}

From source file:Main.java

private static boolean isMobileConnected(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (info != null && info.isConnected()) {
        return true;
    }/*  w w  w .  j  ava2 s . co  m*/

    return false;
}

From source file:Main.java

/**
 *
 * @return/*from   w ww.  j a  va 2s  .c  o  m*/
 */
public static boolean isMobile(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (networkInfo != null) {
        return networkInfo.isConnected();
    }
    return false;
}

From source file:Main.java

private static boolean isConnectionFast(int type, int subType) {
    if (type == ConnectivityManager.TYPE_WIFI) {
        return true;
    } else if (type == ConnectivityManager.TYPE_MOBILE) {
        switch (subType) {
        case TelephonyManager.NETWORK_TYPE_1xRTT:
            return false; // ~ 50-100 kbps
        case TelephonyManager.NETWORK_TYPE_CDMA:
            return false; // ~ 14-64 kbps
        case TelephonyManager.NETWORK_TYPE_EDGE:
            return false; // ~ 50-100 kbps
        case TelephonyManager.NETWORK_TYPE_GPRS:
            return false; // ~ 100 kbps
        case TelephonyManager.NETWORK_TYPE_EVDO_0:
            return false; // ~25 kbps 
        case TelephonyManager.NETWORK_TYPE_LTE:
            return true; // ~ 400-1000 kbps
        case TelephonyManager.NETWORK_TYPE_EVDO_A:
            return true; // ~ 600-1400 kbps
        case TelephonyManager.NETWORK_TYPE_HSDPA:
            return true; // ~ 2-14 Mbps
        case TelephonyManager.NETWORK_TYPE_HSPA:
            return true; // ~ 700-1700 kbps
        case TelephonyManager.NETWORK_TYPE_HSUPA:
            return true; // ~ 1-23 Mbps
        case TelephonyManager.NETWORK_TYPE_UMTS:
            return true; // ~ 400-7000 kbps
        case TelephonyManager.NETWORK_TYPE_EHRPD:
            return true; // ~ 1-2 Mbps
        case TelephonyManager.NETWORK_TYPE_EVDO_B:
            return true; // ~ 5 Mbps
        case TelephonyManager.NETWORK_TYPE_HSPAP:
            return true; // ~ 10-20 Mbps
        case TelephonyManager.NETWORK_TYPE_IDEN:
            return true; // ~ 10+ Mbps
        case TelephonyManager.NETWORK_TYPE_UNKNOWN:
        default://w  w  w .j  a va2s .c  om
            return false;
        }
    } else {
        return false;
    }
}

From source file:Main.java

public static int availableCommunication(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    boolean isWifiAvail = ni.isAvailable();
    boolean isWifiConn = ni.isConnected();
    ni = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    boolean isMobileAvail = ni.isAvailable();
    boolean isMobileConn = ni.isConnected();
    String status = "WiFi\nAvail = " + isWifiAvail + "\nConn = " + isWifiConn + "\nMobile\nAvail = "
            + isMobileAvail + "\nConn = " + isMobileConn + "\n";
    Log.d("Communication State! ", status);
    if (isWifiAvail == true && isWifiConn == true) {
        return ConnectivityManager.TYPE_WIFI;
    } else if (isMobileAvail == true && isMobileConn == true) {
        return ConnectivityManager.TYPE_MOBILE;
    } else {//w  w  w. j ava2 s.  c  o m
        return -1;
    }
}

From source file:Main.java

public static boolean isNetworkAvaliable(Context context) {
    boolean isNetwokAvailable = false;
    ConnectivityManager connectionManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiInfo = connectionManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo mobileInfo = connectionManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (wifiInfo != null && wifiInfo.isConnected()) {
        isNetwokAvailable = true;//from ww  w .  j  a  v a  2s.c o  m
    } else if (mobileInfo != null && mobileInfo.isConnected()) {
        isNetwokAvailable = true;
    } else {

        isNetwokAvailable = false;
    }

    return isNetwokAvailable;

}

From source file:Main.java

static public String getMacAddress(Context context, ConnectivityManager connMananger) {
    Log.d(TAG, "getMacAddress");

    String macAddress = "";

    NetworkInfo info = connMananger.getActiveNetworkInfo();
    if (info != null && info.isConnected()) {
        int type = info.getType();

        Log.d(TAG, "connected type = " + type + " name " + info.getTypeName());

        if (type == ConnectivityManager.TYPE_WIFI) {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            macAddress = wifiInfo.getMacAddress();
        } else if (type == ConnectivityManager.TYPE_ETHERNET || type == ConnectivityManager.TYPE_MOBILE) {
            String addressFileName = "/sys/class/net/eth0/address";
            File addressFile = new File(addressFileName);
            Log.d(TAG, "1");

            if (addressFile.exists()) {
                Log.d(TAG, "2");
                macAddress = readString(addressFile);
                Log.d(TAG, macAddress);//from w  w w . j a  va  2  s .c  o m
            } else {
                addressFileName = "/sys/class/net/eth1/address";
                addressFile = new File(addressFileName);
                if (addressFile.exists()) {
                    macAddress = readString(addressFile);
                    Log.d(TAG, macAddress);
                }
            }
        } else {
            //other type;
        }
    }
    return macAddress;
}

From source file:Main.java

public static int getNetType(Context context) {
    int netWorkType = NETWORK_CLASS_UNKNOWN;

    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected()) {
        int type = networkInfo.getType();

        if (type == ConnectivityManager.TYPE_WIFI) {
            netWorkType = NETWORK_WIFI;/*from   www.j  a  v  a 2s .co  m*/
        } else if (type == ConnectivityManager.TYPE_MOBILE) {
            TelephonyManager telephonyManager = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            switch (telephonyManager.getNetworkType()) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return NETWORK_CLASS_2_G;
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            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:
                return NETWORK_CLASS_3_G;

            case TelephonyManager.NETWORK_TYPE_LTE:
                return NETWORK_CLASS_4_G;
            default:
                return NETWORK_CLASS_UNKNOWN;
            }
        }
    }

    return netWorkType;
}

From source file:Main.java

/**
 * Check for 3g internet connectivity//from w  w  w  . j av  a  2 s . c o  m
 *
 * @param context context
 * @return hasInternet boolean
 */
public static boolean has3gInternetAccess(@NonNull Context context) {
    try {
        boolean has3gInternet = false;
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mobile != null && mobile.isAvailable() && // !ic_mobile.isRoaming() &&
                networkInfo != null && networkInfo.isConnectedOrConnecting()) {

            has3gInternet = true;
        }

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