参考网址: https://www.cnblogs.com/fnlingnzb-learner/p/7580691.html
https://www.jianshu.com/p/c46b6c9b8990
/**
* 获取手机IMEI号
*
* 需要动态权限: android.permission.READ_PHONE_STATE
*/
public static String getIMEI(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
@SuppressLint("MissingPermission")
String imei = telephonyManager.getDeviceId();
@SuppressLint({"NewApi", "MissingPermission", "LocalSuppress"})
String str = telephonyManager.getImei();
String id = Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
return imei;
}
//没有量产的手机(水货)它可能有无效的IMEI,如:0000000000000;通过上边的方法有可能获取到,也有可能获取不到
/**这个方法一定可以获取到
* @param slotId slotId为卡槽Id,它的值为 0、1;
* @return
*/
public static String getIMEI(Context context, int slotId) {
try {
TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Method method = manager.getClass().getMethod("getImei", int.class);
String imei = (String) method.invoke(manager, slotId);
return imei;
} catch (Exception e) {
return "";
}
}