在有些手机(vivo)通过以下方法获取android手机的imei会返回一串英文:
public static String getDeviceIMEI(Context ctx) {
TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
if (tm != null && ActivityCompat.checkSelfPermission(ctx, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
return tm.getDeviceId();
} else {
return "";
}
}
改用反射的反射,ok
/**
*slotId 卡槽
*/
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 "";
}
}
注意添加权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />