Example usage for android.content Context TELEPHONY_SERVICE

List of usage examples for android.content Context TELEPHONY_SERVICE

Introduction

In this page you can find the example usage for android.content Context TELEPHONY_SERVICE.

Prototype

String TELEPHONY_SERVICE

To view the source code for android.content Context TELEPHONY_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.telephony.TelephonyManager for handling management the telephony features of the device.

Usage

From source file:Main.java

public static String getDeviceId(Context context) {
    // IMEI, if present
    TelephonyManager telephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    String imei = telephonyMgr.getDeviceId(); // Requires READ_PHONE_STATE
    if (imei != null)
        return imei;

    String devId = "35" + //we make this look like a valid IMEI
            Build.BOARD.length() % 10 + Build.BRAND.length() % 10 + Build.CPU_ABI.length() % 10
            + Build.DEVICE.length() % 10 + Build.DISPLAY.length() % 10 + Build.HOST.length() % 10
            + Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 + Build.MODEL.length() % 10
            + Build.PRODUCT.length() % 10 + Build.TAGS.length() % 10 + Build.TYPE.length() % 10
            + Build.USER.length() % 10; //13 digits

    return devId;
}

From source file:Main.java

/**
 * Gets the voicemail tag from Telephony Manager.
 * @param context Current application context
 * @return Voicemail tag, the alphabetic identifier associated with the voice mail number.
 *//*from   w  ww.  ja va 2  s  . c o m*/
public static String getVoiceMailAlphaTag(Context context) {
    final TelephonyManager telephonyManager = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
    final String voiceMailLabel = telephonyManager.getVoiceMailAlphaTag();
    return voiceMailLabel;
}

From source file:Main.java

/**
 * Whether is fast mobile network/*w  w w  . j  av a 2s.c  o  m*/
 */

private static boolean isFastMobileNetwork(Context context) {
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (telephonyManager == null) {
        return false;
    }

    switch (telephonyManager.getNetworkType()) {
    case TelephonyManager.NETWORK_TYPE_1xRTT:
        return false;
    case TelephonyManager.NETWORK_TYPE_CDMA:
        return false;
    case TelephonyManager.NETWORK_TYPE_EDGE:
        return false;
    case TelephonyManager.NETWORK_TYPE_EVDO_0:
        return true;
    case TelephonyManager.NETWORK_TYPE_EVDO_A:
        return true;
    case TelephonyManager.NETWORK_TYPE_GPRS:
        return false;
    case TelephonyManager.NETWORK_TYPE_HSDPA:
        return true;
    case TelephonyManager.NETWORK_TYPE_HSPA:
        return true;
    case TelephonyManager.NETWORK_TYPE_HSUPA:
        return true;
    case TelephonyManager.NETWORK_TYPE_UMTS:
        return true;
    case TelephonyManager.NETWORK_TYPE_EHRPD:
        return true;
    case TelephonyManager.NETWORK_TYPE_EVDO_B:
        return true;
    case TelephonyManager.NETWORK_TYPE_HSPAP:
        return true;
    case TelephonyManager.NETWORK_TYPE_IDEN:
        return false;
    case TelephonyManager.NETWORK_TYPE_LTE:
        return true;
    case TelephonyManager.NETWORK_TYPE_UNKNOWN:
        return false;
    default:
        return false;
    }
}

From source file:Main.java

public static Map<String, String> getNetworkInfo(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    Map<String, String> map = new HashMap();
    NetworkInfo info = cm.getActiveNetworkInfo();
    if ((info == null) || (!info.isConnectedOrConnecting()) || (withinInBlackList())) {
        map.put("access_subtype", "offline");
        map.put("access", "offline");
        map.put("carrier", "");
    } else {//ww  w .j ava  2s  . c om
        map.put("access_subtype", info.getSubtypeName());
        map.put("access", cleanNetworkTypeName(info.getTypeName()));
        TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        String carrierName = manager.getNetworkOperatorName();
        map.put("carrier", carrierName);
    }
    return map;
}

From source file:Main.java

/**
 * this generate the device Id/*ww w  .java 2s . c o m*/
 *
 * @param baseContext
 * @param contentResolver
 * @return
 */
//http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id
public static String generateDeviceId(Context baseContext, ContentResolver contentResolver) {

    final TelephonyManager tm = (TelephonyManager) baseContext.getSystemService(Context.TELEPHONY_SERVICE);

    final String tmDevice, tmSerial, androidId;
    tmDevice = String.valueOf(tm.getDeviceId());
    tmSerial = String.valueOf(tm.getSimSerialNumber());
    androidId = String.valueOf(android.provider.Settings.Secure.getString(contentResolver,
            android.provider.Settings.Secure.ANDROID_ID));

    UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
    return deviceUuid.toString();

}

From source file:Main.java

public static String getDeviceId(Context context) {
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    String uuid = tm.getDeviceId();
    if (uuid == null) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        String mac = wifiManager.getConnectionInfo().getMacAddress();
        if (mac == null)
            throw new IllegalStateException();
        return mac;
    }//from   ww  w  .  j  a  v  a2 s  . c om
    return uuid;
}

From source file:Main.java

public static boolean isLowBandwithNetwork(Context applicationContext) {

    //check for wifi
    ConnectivityManager connMgr = (ConnectivityManager) applicationContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (!wifi.isConnectedOrConnecting()) {
        //if no wifi, check if we are on GPRS or EDGE
        TelephonyManager tm = (TelephonyManager) applicationContext.getSystemService(Context.TELEPHONY_SERVICE);
        if (tm != null && (tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_EDGE
                || tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_GPRS)) {
            return true;
        }//ww w  . j av  a 2 s  .  c o m
    }
    return false;
}

From source file:Main.java

public static String getNetworkTypeName(Context context) {
    int kind = 0;
    try {/*  w w w. j  a v a 2  s.c  o  m*/
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        kind = tm.getNetworkType();
    } catch (Exception e) {

    }
    return String.valueOf(kind);
}

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  w w  w  .  j  av a 2s  .c  o 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

/**
 * /*from ww w  .jav a  2  s. 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;
}