TelephonyManager.getDeviceId(0)返回不同的结果 [英] TelephonyManager.getDeviceId(0) returns different results

查看:396
本文介绍了TelephonyManager.getDeviceId(0)返回不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某些特定原因,我需要在Android应用程序中获取IMEI.这是我使用的代码:

For some specific reasons I need to get the IMEI at some point in my Android app. Here is the code I use:

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

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M){
    // API available only >= 6.0
    // Get first slot to avoid issue if we have multiple sim cards
    String imei = tm.getDeviceId(0);
}
else
{
    String imei = tm.getDeviceId();
}

在大多数情况下,它都可以正常工作.但是,某些设备(例如Huawei Honor 7)提供双卡双待功能.在设置中,用户可以在两个支持3G/4G的SIM卡之间进行切换.

It works fine in most cases. However some devices (like the Huawei Honor 7) offer dual-sim capability. In the settings, the user has the possibility to switch between the two SIM cards for 3G/4G support.

当我有两张SIM卡并且执行该切换时,我收到的IMEI是不同的.

When I have two SIM cards and I do that switch, the IMEI I get is different.

据我所知,IMEI与物理插槽有关,因此不应更改.从构造函数看,这看起来像是不好的实现.

As far as I know, the IMEI is related to a physical slot and should not change. This looks like bad implementation from the constructor.

有什么解决方法的想法吗?

Any idea for a workaround?

推荐答案

正如您所说,IMEI/设备ID已标记到sim插槽.

As you said IMEI/Device Id is tagged to sim slot.

对于双SIM卡手机,有三个IMEI值(每个插槽一个)和IMEI-SV.

For dual SIM phones there are three IMEI values(one for each slot) and IMEI-SV.

假设广告位1的IMEI是:123456789012345

Let’s say IMEI for slot 1 is: 123456789012345

插槽2的IMEI为:012500123456789

IMEI for slot 2 is: 012500123456789

根据情况,以下是telephonyManagerObj.getDeviceId()返回的值:

Depending on the scenarios, following is the returned value by telephonyManagerObj.getDeviceId():

  1. 没有SIM卡时,该方法将返回插槽1的IMEI,即123456789012345
  2. 如果插槽1中有SIM卡,则该方法将返回插槽1的IMEI,即123456789012345
  3. 两个插槽中都装有SIM卡时,该方法将返回插槽1的IMEI,即123456789012345
  4. 但是,如果只有插槽2中有SIM卡,则该方法将返回插槽2的IMEI,即012500123456789
  5. 我发现在一台设备上,当我将SIM卡错误地插入插槽1时,该方法返回了IMEI-SV

一种保持一致性的解决方法是,一旦成功设法将IMEI存储在SharedPreference/Sqlite中.

One work around to maintain consistency is to store the IMEI in SharedPreference/Sqlite once you successfully managed to retrieve it.

因此,当您在代码中需要IMEI值时,可以首先检查其在本地存储中是否可用.如果不可用,则检索IMEI并将其存储以备下次使用.

So when you need IMEI value in your code, you can first check if its available in your local storage. If not available then retrieve the IMEI and store it for next time usage.

请注意, getDeviceId()具有已在Android O中弃用.请检查如此以寻找替代方法

Be careful, getDeviceId() has been deprecated in Android O. Check this so for alternative

这篇关于TelephonyManager.getDeviceId(0)返回不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆