简体   繁体   English

在android中以编程方式检索双SIM卡的IMEI号码

[英]Programmatically retrieve IMEI number for dual SIM in android

For single SIM following code works:对于单 SIM 卡,以下代码有效:

TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String imei= tm.getDeviceId();

For dual SIM I tried code on following link:对于双 SIM 卡,我在以下链接上尝试了代码:

Android : Check whether the phone is dual SIM Android : 检查手机是否为双卡

But it didnt work for me.但它对我不起作用。

Let me know if any other solutions possible.如果有其他可能的解决方案,请告诉我。

Try using getDeviceId(int slotId) added in API level 23.尝试使用 API 级别 23 中添加的getDeviceId(int slotId)

Returns the unique device ID of a subscription, for example, the IMEI for GSM and the MEID for CDMA phones.返回订阅的唯一设备 ID,例如,GSM 的 IMEI 和 CDMA 手机的 MEID。 Return null if device ID is not available.如果设备 ID 不可用,则返回 null。

Requires Permission: READ_PHONE_STATE需要权限: READ_PHONE_STATE

We can check whether phone single or dual sim by using Android API and IMEI for each sim Card我们可以通过使用Android API和每个sim卡的IMEI来检查手机是单卡还是双卡

TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Log.i("OmSai ", "Single or Dula Sim "+manager.getPhoneCount());
Log.i("OmSai ", "Defualt device ID "+manager.getDeviceId());
Log.i("OmSai ", "Single 1 "+manager.getDeviceId(0));
Log.i("OmSai ", "Single 2 "+manager.getDeviceId(1));
TelephonyManager telephony = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    try {

        Class<?> telephonyClass = Class.forName(telephony.getClass().getName());
        Class<?>[] parameter = new Class[1];
        parameter[0] = int.class;
        Method getFirstMethod = telephonyClass.getMethod("getDeviceId", parameter);
        Log.d("SimData", getFirstMethod.toString());
        Object[] obParameter = new Object[1];
        obParameter[0] = 0;
        TelephonyManager manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        String first = (String) getFirstMethod.invoke(telephony, obParameter);
        Log.d("SimData", "first :" + first);
        obParameter[0] = 1;
        String second = (String) getFirstMethod.invoke(telephony, obParameter);
        Log.d("SimData", "Second :" + second);

    } catch (Exception e) {
        e.printStackTrace();
    }

try using this,this should help getting both device id on android lollipop尝试使用这个,这应该有助于在 android 棒棒糖上获取两个设备 ID

Yes we can get both IMEI numbers Using this below code是的,我们可以使用以下代码获取两个 IMEI 号码

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String imeiNumber1 = tm.getDeviceId(1); //(API level 23)   
String imeiNumber2 = tm.getDeviceId(2);
TelephonyManager first = (TelephonyManager) getFirstMethod.invoke(null, obParameter);

Log.d(TAG, "Device Id: " + first.getDeviceId() + ", device status: " + first.getSimState() + ", operator: " + first.getNetworkOperator() + "/" + first.getNetworkOperatorName());

obParameter[0] = 1;
TelephonyManager second = (TelephonyManager) getFirstMethod.invoke(null, obParameter);

Log.d(TAG, "Device Id: " + second.getDeviceId() + ", device status: " + second.getSimState()+ ", operator: " + second.getNetworkOperator() + "/" + second.getNetworkOperatorName());

AFAIK it is not possible. AFAIK 这是不可能的。 The java reflection, you used could work for some devices but not all.您使用的 java 反射可以用于某些设备,但不是所有设备。 However there might be some manufacturer specific API's that allows for this.但是,可能有一些制造商特定的 API 允许这样做。

Quoting Commonsware ,引用Commonsware ,

Android does not support multiple SIMs, at least from the SDK. Android 不支持多个 SIM 卡,至少在 SDK 中是这样。 Device manufacturers who have created multi-SIM devices are doing so on their own.创建多 SIM 卡设备的设备制造商正在自行开发。 You are welcome to contact your device manufacturer and see if they have an SDK add-on or something that allows you to access the second SIM.欢迎您联系您的设备制造商,看看他们是否有 SDK 插件或允许您访问第二张 SIM 卡的东西。

Yes Android currently not supported Dual Sim.是的,Android 目前不支持双卡。 But you can retrive all possible details by using Java reflection.但是您可以通过使用 Java 反射来检索所有可能的细节。

I research for fetching dual SIMs details and it works on bellowed devices我研究获取双 SIM 卡的详细信息,它适用于以下设备
Samsung Galaxy Neo三星 Galaxy Neo
Moto E摩托E
Micromax A52微麦克A52
Micromax Canvas Micromax 帆布
Linovo P780力诺P780
HTC Dreem宏达梦
Moto G摩托G
LG LG
HUAWEI Y520-U22华为Y520-U22
LG-P705 LG-P705
Sony ST26i索尼 ST26i
I successfully get dual SIM Detials from above models我成功地从上述型号获得双 SIM 卡详细信息

You can use this method to get both imei.您可以使用此方法获取两个imei。 Sorry for inconvenience.对造成的不便表示歉意。 I was in a hurry.我当时很急。

public static void samsungTwoSims(Context context) {
    TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

try{

        Class<?> telephonyClass = Class.forName(telephony.getClass().getName());

        Class<?>[] parameter = new Class[1];
        parameter[0] = int.class;
        Method getFirstMethod = telephonyClass.getMethod("getDefault", parameter);

        Log.d(TAG, getFirstMethod.toString());

        Object[] obParameter = new Object[1];
        obParameter[0] = 0;
        TelephonyManager first = (TelephonyManager) getFirstMethod.invoke(null, obParameter);

        Log.d(TAG, "Device Id: " + first.getDeviceId() + ", device status: " + first.getSimState() + ", operator: " + first.getNetworkOperator() + "/" + first.getNetworkOperatorName());

        obParameter[0] = 1;
        TelephonyManager second = (TelephonyManager) getFirstMethod.invoke(null, obParameter);

        Log.d(TAG, "Device Id: " + second.getDeviceId() + ", device status: " + second.getSimState()+ ", operator: " + second.getNetworkOperator() + "/" + second.getNetworkOperatorName());
    } catch (Exception e) {
        e.printStackTrace();
    }   
}

You can IMEI in Android O or above.您可以在 Android O 或更高版本中使用 IMEI。

Set<String> deviceIdList = new HashSet<>();
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int phoneCount = telephonyManager.getPhoneCount();
for (int i = 0; i < phoneCount; i++) {
   deviceIdList.add(telephonyManager.getImei(i));
}

Steps: 1 > You must have READ_PHONE_STATE Permission enabled步骤: 1 > 您必须启用 READ_PHONE_STATE 权限

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

2 > For Android SDK v23<= get SIM 1 & SIM 2 IMEI by this: 2 > 对于 Android SDK v23<= 通过以下方式获取 SIM 1 和 SIM 2 IMEI:

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    Log.e("IMEI---1:", tm.getDeviceId(0) );
    Log.e("IMEI---2:", tm.getDeviceId(1) );

Try following code to get IMEI Number for an Android Device:尝试使用以下代码获取 Android 设备的 IMEI 号码:

telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);

    btn_imei.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        public void onClick(View v) {
            if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) 
            {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_CODE);
                return;
            }

            StringBuilder stringBuilder = new StringBuilder();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                for (int i = 0; i < telephonyManager.getPhoneCount(); i++) {
                    stringBuilder.append(telephonyManager.getImei(i));
                    stringBuilder.append("\n");
                }
            }
            txt_imei.setText(stringBuilder.toString());
        }
    });

TelephonyInfo telephonyInfo = TelephonyInfo.getInstance(this); TelephonyInfo telephonyInfo = TelephonyInfo.getInstance(this);

    String imeiSIM1 = telephonyInfo.getImsiSIM1();
    String imeiSIM2 = telephonyInfo.getImsiSIM2();

    boolean isSIM1Ready = telephonyInfo.isSIM1Ready();
    boolean isSIM2Ready = telephonyInfo.isSIM2Ready();

    boolean isDualSIM = telephonyInfo.isDualSIM();

    TextView tv = (TextView) findViewById(R.id.txt_imei);
    tv.setText(" IME1 : " + imeiSIM1 + "\n" +
            " IME2 : " + imeiSIM2 + "\n" +
            " IS DUAL SIM : " + isDualSIM + "\n" +
            " IS SIM1 READY : " + isSIM1Ready + "\n" +
            " IS SIM2 READY : " + isSIM2Ready + "\n");

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM