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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22524413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:07:01  来源:igfitidea点击:

Programmatically retrieve IMEI number for dual SIM in android

androidimeidual-sim

提问by NehaC

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.

如果有其他可能的解决方案,请告诉我。

回答by Vijay Shelke

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));

回答by Dheeraj Vepakomma

Try using getDeviceId(int slotId)added in API level 23.

尝试使用getDeviceId(int slotId)在 API 级别 23 中添加的。

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

Requires Permission: READ_PHONE_STATE

返回订阅的唯一设备 ID,例如,GSM 的 IMEI 和 CDMA 手机的 MEID。如果设备 ID 不可用,则返回 null。

需要权限: READ_PHONE_STATE

回答by Akshay Shah

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

回答by user7036069

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);

回答by Ravindra Barnwal

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());

回答by Brijesh Gupta

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));
}

回答by abir99

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) );

回答by Vishv Shroff

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());
        }
    });

回答by user6676551

You can use this method to get both imei. Sorry for inconvenience. I was in a hurry.

您可以使用此方法获取两个imei。对造成的不便表示歉意。我当时很急。

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();
    }   
}

回答by Sahil Mahajan Mj

AFAIK it is not possible. The java reflection, you used could work for some devices but not all. However there might be some manufacturer specific API's that allows for this.

AFAIK 这是不可能的。您使用的 Java 反射可以适用于某些设备,但不是全部。但是,可能有一些制造商特定的 API 允许这样做。

Quoting Commonsware,

引用Commonsware,

Android does not support multiple SIMs, at least from the SDK. Device manufacturers who have created multi-SIM devices are doing so on their own. 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.

Android 不支持多个 SIM 卡,至少在 SDK 中是这样。创建多 SIM 卡设备的设备制造商正在自行开发。欢迎您联系您的设备制造商,看看他们是否有 SDK 插件或允许您访问第二张 SIM 卡的东西。