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

private static String mobileNetworkType(Context context) {
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (telephonyManager == null) {
        return "TM==null";
    }//  w ww.  j  a  v a  2s . com
    switch (telephonyManager.getNetworkType()) {
    case TelephonyManager.NETWORK_TYPE_1xRTT:// ~ 50-100 kbps
        return "2G";
    case TelephonyManager.NETWORK_TYPE_CDMA:// ~ 14-64 kbps
        return "2G";
    case TelephonyManager.NETWORK_TYPE_EDGE:// ~ 50-100 kbps
        return "2G";
    case TelephonyManager.NETWORK_TYPE_EVDO_0:// ~ 400-1000 kbps
        return "3G";
    case TelephonyManager.NETWORK_TYPE_EVDO_A:// ~ 600-1400 kbps
        return "3G";
    case TelephonyManager.NETWORK_TYPE_GPRS:// ~ 100 kbps
        return "2G";
    case TelephonyManager.NETWORK_TYPE_HSDPA:// ~ 2-14 Mbps
        return "3G";
    case TelephonyManager.NETWORK_TYPE_HSPA:// ~ 700-1700 kbps
        return "3G";
    case TelephonyManager.NETWORK_TYPE_HSUPA: // ~ 1-23 Mbps
        return "3G";
    case TelephonyManager.NETWORK_TYPE_UMTS:// ~ 400-7000 kbps
        return "3G";
    case TelephonyManager.NETWORK_TYPE_EHRPD:// ~ 1-2 Mbps
        return "3G";
    case TelephonyManager.NETWORK_TYPE_EVDO_B: // ~ 5 Mbps
        return "3G";
    case TelephonyManager.NETWORK_TYPE_HSPAP:// ~ 10-20 Mbps
        return "3G";
    case TelephonyManager.NETWORK_TYPE_IDEN:// ~25 kbps
        return "2G";
    case TelephonyManager.NETWORK_TYPE_LTE:// ~ 10+ Mbps
        return "4G";
    case TelephonyManager.NETWORK_TYPE_UNKNOWN:
        return "UNKNOWN";
    default:
        return "4G";
    }
}

From source file:Main.java

/**
 * Get device Id<br>/*from ww  w .ja va  2 s .c  om*/
 * <b>Will work only on mobile and NOT tablets</b>
 * 
 * @param context
 * @return
 */
public static String getDeviceId(Context context) {
    final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    String deviceid = tm.getDeviceId();
    if (deviceid == null) {
        deviceid = getUniqueId(context);
    }
    return deviceid;
}

From source file:Main.java

public static void getScanWifiResults(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    List<ScanResult> wifiResults = wifiManager.getScanResults();
    for (ScanResult wifi : wifiResults) {
        //         LogUtil.log(TAG, Log.DEBUG, wifi.toString());
    }//from   w w w.j  av a 2s.c o m

    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    //      LogUtil.log(TAG, Log.DEBUG, TelephonyManager.PHONE_TYPE_GSM + "----" + tm.getPhoneType());
    List<NeighboringCellInfo> cellResults = tm.getNeighboringCellInfo();
    for (NeighboringCellInfo cell : cellResults) {
        //         LogUtil.log(TAG, Log.DEBUG, cell.getCid() + "-" + cell.getLac() + "-" + cell.getRssi() + "-" + cell.getPsc()
        //               + "-" + cell.getNetworkType());
    }

    //      LogUtil.log(TAG, Log.DEBUG, getProviderName(context).getText());
}

From source file:Main.java

/**
 * Gets an unique device Id depending on the sdk version
 *
 * @param context/*w  w  w  .  j  a  v  a 2  s.  c  o m*/
 * @return
 */
public static String getUniqueDeviceId(Context context) {
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) {
        return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    } else {
        final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getDeviceId();
    }
}

From source file:Main.java

public static boolean checkNetworkConnection(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    State wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    if (wifi == State.CONNECTED) {
        return true;
    }//from   w  ww .ja  v a  2s  .c om
    State mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
    if (mobile == State.CONNECTED) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        int type = tm.getNetworkType();
        if (type != TelephonyManager.NETWORK_TYPE_CDMA && type != TelephonyManager.NETWORK_TYPE_EDGE
                && type != TelephonyManager.NETWORK_TYPE_GPRS) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * Reads the phone state in order to get SIM serial number and AndroidID, those values are saved in static
 * attributes in this class/*from   ww w.j  a v a  2 s  .  c om*/
 *
 * @param context
 */
public static void readPhoneState(Context context) {

    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    //Get SIM serial number
    SIM_SERIAL_NUMBER = telephonyManager.getSimSerialNumber();
    if (SIM_SERIAL_NUMBER == null) {
        SIM_SERIAL_NUMBER = "";
    }

    /*
     * Settings.Secure.ANDROID_ID returns the unique DeviceID
     * Works for Android 2.2 and above
     */
    ANDROID_ID = getUniqueDeviceId(context);
}

From source file:Main.java

/**
 * Requires {@link android.Manifest.permission#READ_PHONE_STATE} permission.
 *
 * @param context/*from w  w  w  . j  av  a 2s. c om*/
 * @return
 */
public static boolean isMobileDataEnabled(Context context) {
    boolean mobileDataEnabled = false; // Assume disabled

    if (Build.VERSION.SDK_INT >= 21) {
        try {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Class<?> tmClass = Class.forName(tm.getClass().getName());
            Method method = tmClass.getDeclaredMethod("getDataEnabled");
            method.setAccessible(true);
            mobileDataEnabled = (Boolean) method.invoke(tm);
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalArgumentException
                | IllegalAccessException | InvocationTargetException ex) {
            ex.printStackTrace();
        }
    } else {
        try {
            ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            Class<?> cmClass = Class.forName(cm.getClass().getName());
            Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
            method.setAccessible(true);
            mobileDataEnabled = (Boolean) method.invoke(cm);
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalArgumentException
                | IllegalAccessException | InvocationTargetException ex) {
            ex.printStackTrace();
        }
    }

    return mobileDataEnabled;
}

From source file:Main.java

/***
 * getIMEI : read the IMEI number of device The IMEI (14 decimal digits plus
 * a check digit) or IMEISV (16 digits) includes information on the origin,
 * model, and serial number of the device. The structure of the IMEI/SV are
 * specified in 3GPP TS 23.003/*www.  j a  v a 2 s.  c o  m*/
 * 
 * @param activity
 * @return 16 digit IMEI number of device ( padded with zeros)
 */
public static String getIMEI(Activity activity) {
    try {
        TelephonyManager telephonyManager = (TelephonyManager) activity
                .getSystemService(Context.TELEPHONY_SERVICE);
        String imei = telephonyManager.getDeviceId();
        if (imei == null)
            imei = Secure.getString(activity.getContentResolver(), Secure.ANDROID_ID);
        return imei;
    } catch (Exception e) {
    }
    return "EEEE-EEEE-EEEE-E";
}

From source file:Main.java

/**
 * @param context//from www.  j a v a2  s  .c o m
 * @return String
 * @throws
 * @Title: getPhoneId
 * @Description: TODO
 */
public static String getPhoneId(Context context) {
    if (context == null) {
        return "";
    }
    if (checkPermission(context, "android.permission.READ_PHONE_STATE")) {
        String phoneId = "";
        if (checkPhoneState(context)) {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            phoneId = tm.getDeviceId();
            if (phoneId != null) {
                return phoneId;
            }
        }
    }
    return "";
}

From source file:Main.java

public static String getDeviceInfo(Context context) {
    try {// w  ww  .  j  a  v a 2  s  . c om
        org.json.JSONObject json = new org.json.JSONObject();
        android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        String device_id = null;
        if (checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
            device_id = tm.getDeviceId();
        }
        String mac = null;
        FileReader fstream = null;
        try {
            fstream = new FileReader("/sys/class/net/wlan0/address");
        } catch (FileNotFoundException e) {
            fstream = new FileReader("/sys/class/net/eth0/address");
        }
        BufferedReader in = null;
        if (fstream != null) {
            try {
                in = new BufferedReader(fstream, 1024);
                mac = in.readLine();
            } catch (IOException e) {
            } finally {
                if (fstream != null) {
                    try {
                        fstream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        json.put("mac", mac);
        if (TextUtils.isEmpty(device_id)) {
            device_id = mac;
        }
        if (TextUtils.isEmpty(device_id)) {
            device_id = android.provider.Settings.Secure.getString(context.getContentResolver(),
                    android.provider.Settings.Secure.ANDROID_ID);
        }
        json.put("device_id", device_id);
        return json.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}