双卡双待 getDeviceId unique device ID IMEI 不唯一 会变问题

来源:互联网 发布:python高手之路 第3版 编辑:程序博客网 时间:2024/06/04 19:55

最近接到用户反馈无法登录的情况越来越多,因为我们的app设计上是不能换手机用,也就是绑定了唯一的设备ID。从反馈上来看,有一个线索是大部分是双卡双待的用户出现这个问题,并且切换过SIM卡。看来getDeviceId这个方法在双卡双待手机上获取IMEI还是有问题的。

getDeviceId方式的注释:

Returns the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones. Return null if device ID is not available.

注释上已经说明了获取的IMEI不会绝对得唯一,GSM CDMA是sim卡网络制式,也就说getDeviceId会根据网络制式返回不同的IMEI,那我们要修正这个问题,就要获取所有的IMEI。继续看文档,果然,发现API版本23新增了一个方法 public String getDeviceId(int slotId),参数slotId文档没有详细写应该怎么填,查看源码发现如下一段常量。

/** No phone radio. */    public static final int PHONE_TYPE_NONE = PhoneConstants.PHONE_TYPE_NONE;    /** Phone radio is GSM. */    public static final int PHONE_TYPE_GSM = PhoneConstants.PHONE_TYPE_GSM;    /** Phone radio is CDMA. */    public static final int PHONE_TYPE_CDMA = PhoneConstants.PHONE_TYPE_CDMA;    /** Phone is via SIP. */    public static final int PHONE_TYPE_SIP = PhoneConstants.PHONE_TYPE_SIP;

这样我们就能获取所有的IMEI了,如下代码:

public static String getWholeImei() {String imeiStr = null,imeiStr1= ",",imeiStr2= ",",imeiStr3= ",",imeiStr4= ",";try {imeiStr = ((TelephonyManager) MyApp.getInstance().getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();imeiStr1 += ((TelephonyManager) MyApp.getInstance().getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId(TelephonyManager.PHONE_TYPE_NONE);imeiStr2 += ((TelephonyManager) MyApp.getInstance().getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId(TelephonyManager.PHONE_TYPE_GSM);imeiStr3 += ((TelephonyManager) MyApp.getInstance().getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId(TelephonyManager.PHONE_TYPE_CDMA);imeiStr4 += ((TelephonyManager) MyApp.getInstance().getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId(TelephonyManager.PHONE_TYPE_SIP);} catch (Throwable e) {e.printStackTrace();}LogUtil.i(imeiStr+imeiStr1+imeiStr2+imeiStr3+imeiStr4);return imeiStr+=imeiStr1+=imeiStr2+=imeiStr3+=imeiStr4;}


需要注意的一点是 虽然API文档上显示此方法是api23,也就是android L 6.0才加的,但是笔者在5.0以上的手机上都能调用,5.0以下才会报NoSuchMethod错误,所以最好的做法就是捕捉ERROR,避免app崩溃。


最后举个例子上个结果:

获取到的IMEI:whole_imei=869315022916292,A000005E67BFA4,869315023576038,A000005E67BFA4,A000005E67BFA4

然后上个黑科技 手机拨号界面输入*#06# 就能查看IMEI MEID之类的信息哦


证明我们获得了本手机上的所有IMEI。


0 0
原创粉丝点击