Java Code Examples for android.telephony.TelephonyManager#getImei()

The following examples show how to use android.telephony.TelephonyManager#getImei() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeviceUtils.java    From Android-utils with Apache License 2.0 6 votes vote down vote up
@SuppressLint("HardwareIds")
@RequiresPermission(READ_PHONE_STATE)
public static String getDeviceId() {
    TelephonyManager tm =
            (TelephonyManager) UtilsApp.getApp().getSystemService(Context.TELEPHONY_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        //noinspection ConstantConditions
        String imei = tm.getImei();
        if (!TextUtils.isEmpty(imei)) return imei;
        String meid = tm.getMeid();
        return TextUtils.isEmpty(meid) ? "" : meid;

    }
    //noinspection ConstantConditions
    return tm.getDeviceId();
}
 
Example 2
Source File: PhoneUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 获取 IMEI 码
 * <pre>
 *     IMEI 是 International Mobile Equipment Identity ( 国际移动设备标识 ) 的简称
 *     IMEI 由 15 位数字组成的「电子串号」它与每台手机一一对应, 而且该码是全世界唯一的
 *     其组成为:
 *     1. 前 6 位数 (TAC) 是「型号核准号码」一般代表机型
 *     2. 接着的 2 位数 (FAC) 是「最后装配号」一般代表产地
 *     3. 之后的 6 位数 (SNR) 是「串号」一般代表生产顺序号
 *     4. 最后 1 位数 (SP) 通常是「0」为检验码, 目前暂备用
 * </pre>
 * @param slotIndex 卡槽索引
 * @return IMEI 码
 */
@RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
public static String getIMEI(final int slotIndex) {
    try {
        TelephonyManager telephonyManager = AppUtils.getTelephonyManager();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (slotIndex == -1) return telephonyManager.getImei();
            return telephonyManager.getImei(slotIndex);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // 反射调用方法
            Class clazz = telephonyManager.getClass();
            Method method = clazz.getDeclaredMethod("getImei");
            method.setAccessible(true);
            return (String) method.invoke(telephonyManager);
        }
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "getIMEI");
    }
    return null;
}
 
Example 3
Source File: PhoneUtils.java    From AndroidUtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * Return the unique device id.
 * <p>If the version of SDK is greater than 28, it will return an empty string.</p>
 * <p>Must hold {@code <uses-permission android:name="android.permission.READ_PHONE_STATE" />}</p>
 *
 * @return the unique device id
 */
@SuppressLint("HardwareIds")
@RequiresPermission(READ_PHONE_STATE)
public static String getDeviceId() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        return "";
    }
    TelephonyManager tm = getTelephonyManager();
    String deviceId = tm.getDeviceId();
    if (!TextUtils.isEmpty(deviceId)) return deviceId;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String imei = tm.getImei();
        if (!TextUtils.isEmpty(imei)) return imei;
        String meid = tm.getMeid();
        return TextUtils.isEmpty(meid) ? "" : meid;
    }
    return "";
}
 
Example 4
Source File: DeviceUtils.java    From Box with Apache License 2.0 5 votes vote down vote up
@SuppressLint({"MissingPermission", "HardwareIds"})
@RequiresPermission(READ_PHONE_STATE)
public static String getIMEI(Context context) {
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (telephonyManager == null) return "";
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? telephonyManager.getImei() : telephonyManager.getDeviceId();
}
 
Example 5
Source File: DeviceUtils.java    From Android-utils with Apache License 2.0 5 votes vote down vote up
@SuppressLint("HardwareIds")
@RequiresPermission(READ_PHONE_STATE)
public static String getIMEI() {
    TelephonyManager tm =
            (TelephonyManager) UtilsApp.getApp().getSystemService(Context.TELEPHONY_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        //noinspection ConstantConditions
        return tm.getImei();
    }
    //noinspection ConstantConditions
    return tm.getDeviceId();
}
 
Example 6
Source File: DeviceUtils.java    From Box with Apache License 2.0 5 votes vote down vote up
@SuppressLint({"MissingPermission", "HardwareIds"})
@RequiresPermission(READ_PHONE_STATE)
public static String getIMEI(Context context) {
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (telephonyManager == null) return "";
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? telephonyManager.getImei() : telephonyManager.getDeviceId();
}
 
Example 7
Source File: DeviceUtil.java    From SimpleProject with MIT License 5 votes vote down vote up
/**
 * 获取手机的IMEI码
 * @param context
 * @return
 */
@SuppressLint("MissingPermission")
public static String getDeviceId(Context context) {
	TelephonyManager phoneManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
	if (phoneManager != null) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
			return phoneManager.getImei();
		} else {
			return phoneManager.getDeviceId();
		}
	}

	return "";
}
 
Example 8
Source File: SensorsDataUtils.java    From sa-sdk-android with Apache License 2.0 5 votes vote down vote up
/**
 * 此方法谨慎修改
 * 插件配置 disableIMEI 会修改此方法
 * 获取IMEI
 *
 * @param context Context
 * @return IMEI
 */
@SuppressLint({"MissingPermission", "HardwareIds"})
public static String getIMEI(Context context) {
    String imei = "";
    try {
        if (!checkHasPermission(context, Manifest.permission.READ_PHONE_STATE)) {
            return imei;
        }

        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (tm != null) {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
                if (tm.hasCarrierPrivileges()) {
                    imei = tm.getImei();
                } else {
                    SALog.d(TAG, "Can not get IMEI info.");
                }
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                imei = tm.getImei();
            } else {
                imei = tm.getDeviceId();
            }
        }
    } catch (Exception e) {
        SALog.printStackTrace(e);
    }
    return imei;
}