Android How to Obtain Dual Card Mobile Phone IMEI Method Example
- 2021-10-15 11:36:08
- OfStack
Foreword:
There is a demand for counting the conversion rate of paid advertisements in the project, and it is necessary to obtain the IMEI of the user's mobile phone. But the most common method on the web is the pit, which is TelephonyManager. getDeviceId (), which may get MEID or a null value. Then I went down with Google and found that the first few answers I searched could not get two IMEI values correctly. Then I see that the next method of getDeviceId () method in TelephonyManager source code is getImei () method, which is only hidden by the system. Using reflection call 1, you can really get two IMEI values, so share 1 here ~ ~
Background:
The code for getting IMEI like 1 is as follows
public static String getIMEI(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String imei = telephonyManager.getDeviceId();
return imei;
}
As noted in the comments in the source code, this method may return IMEI or MEID. The Huawei I used returned MEID. He also has a method getDeviceId with parameters (int slotId). This slotId should be the card slot number, and it may also be possible to get the DeviceId of the dual-card mobile phone, but I didn't try it, because anyway, the IMEI he returned must be the IMEI I want.
Solution:
Use reflection to call getImei () hidden by the system.
/**
* @param slotId slotId Is the card slot Id Which has a value of 0 , 1 ;
* @return
*/
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 "";
}
}
When calling, pass in 0 and 1 respectively, and you can get the IMEI of the dual-card mobile phone.
PS: Method for obtaining IMEI, IMSI and ICCI of main card
/**
* Author: liuqiang
* Time: 2017-08-14 15:28
* Description:
* <p>
* IMEI It is bound to your mobile phone Used to distinguish mobile terminal equipment
* IMSI It is bound to your mobile phone card Valid information for distinguishing mobile users IMSI Is the identity of the user.
* ICCID ICCID Is the identity of the card, which is defined by 20 Bit number composition
* ICCID It's just used to distinguish SIM Card, do not make access to the network authentication. And IMSI When accessing the network, it will be verified in the operator's server.
* https://github.com/android/platform_frameworks_base/blob/master/telephony/java/android/telephony/TelephonyManager.java
*/
@RequiresApi(api = Build.VERSION_CODES.O)
public void check(View view) {
TelephonyManager telephonyManager = (TelephonyManager) this
.getSystemService(TELEPHONY_SERVICE);// Obtain relevant system services
String simOperatorName = telephonyManager.getSimOperatorName();
String imei = telephonyManager.getDeviceId(); // Take out IMEI
String imeiAPI26 = telephonyManager.getImei(); // Take out IMEI Need api26 Above
String tel = telephonyManager.getLine1Number(); // Take out MSISDN , most likely empty
String imsi = telephonyManager.getSubscriberId(); // Take out IMSI
String icc = telephonyManager.getSimSerialNumber(); // Take out ICCID
Log.d("Q_M", " Operator name --" + simOperatorName);
Log.d("Q_M", "IMEI--" + imei);
Log.d("Q_M", "IMEI_API26--" + imeiAPI26);
Log.d("Q_M", "IMSI--" + imsi);
Log.d("Q_M", "ICCID--" + icc);
}