相信關於這個獲取網上有很多例子。我說說的情況吧,我項目使用的api版本是21(android 5.1)。所以沒有網上6.0以上或者8.0方法,可以直接獲取接口。只能用反射,因爲接口在5.1是被屏蔽了。
meid 是電信的一種方式。我們目標是獲取IMEI1作爲唯一碼。但是測試會發現下面的問題。
網上有個很全的解釋:
這個的獲取IMEI 是通過getDeviceId()這個方法獲取。其中帶參數的getDeviceId(0) 是被屏蔽的。
用這個方法獲取,有一種情況獲取不到IMEI 1(就是上圖的第二種)。所以有點問題。
因爲使用IMEI1 纔是單卡和雙卡都會有。作爲唯一碼必須的條件。
2、解決辦法。
我使用getImei(0)方法來反射。借用網上一個代碼修改的。看下面代碼。
package com.readimei;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
public class CTelephoneInfo {
private static final String TAG = CTelephoneInfo.class.getSimpleName();
private String imeiSIM1;// IMEI
private String imeiSIM2;//IMEI
private String meidSIM;
private static CTelephoneInfo CTelephoneInfo;
private static Context mContext;
private CTelephoneInfo() {
}
public synchronized static CTelephoneInfo getInstance(Context context){
if(CTelephoneInfo == null) {
CTelephoneInfo = new CTelephoneInfo();
}
mContext = context;
return CTelephoneInfo;
}
public String getImeiSIM1() {
return imeiSIM1;
}
public String getImeiSIM2() {
return imeiSIM2;
}
public String getMeidSIMSIM2() {
return meidSIM;
}
public void setCTelephoneInfo(){
TelephonyManager telephonyManager = ((TelephonyManager)
mContext.getSystemService(Context.TELEPHONY_SERVICE));
//如果電信卡插在卡1 這個默認獲取是meid
CTelephoneInfo.meidSIM = telephonyManager.getDeviceId();
CTelephoneInfo.imeiSIM1 = "";
CTelephoneInfo.imeiSIM2 = "";
try {
CTelephoneInfo.imeiSIM1 = getOperatorBySlot(mContext, "getImei", 0);
CTelephoneInfo.imeiSIM2 = getOperatorBySlot(mContext, "getImei", 1);
} catch (GeminiMethodNotFoundException e) {
e.printStackTrace();
try {
CTelephoneInfo.imeiSIM1 = getOperatorBySlot(mContext, "getDeviceId", 0);
CTelephoneInfo.imeiSIM2 = getOperatorBySlot(mContext, "getDeviceId", 1);
} catch (GeminiMethodNotFoundException e1) {
e1.printStackTrace();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
private static String getOperatorBySlot(Context context, String predictedMethodName, int slotID)
throws GeminiMethodNotFoundException {
String inumeric = null;
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 getSimID = telephonyClass.getMethod(predictedMethodName, parameter);
Object[] obParameter = new Object[1];
obParameter[0] = slotID;
Object ob_phone = getSimID.invoke(telephony, obParameter);
if(ob_phone != null){
inumeric = ob_phone.toString();
}
} catch (Exception e) {
e.printStackTrace();
throw new GeminiMethodNotFoundException(predictedMethodName);
}
return inumeric;
}
private static class GeminiMethodNotFoundException extends Exception {
/**
*
*/
private static final long serialVersionUID = -3241033488141442594L;
public GeminiMethodNotFoundException(String info) {
super(info);
}
}
}
如果使用getImei(0)方法來反射出現異常,就使用getDeviceId(0)來反射。
IMEI1作爲唯一碼那就這樣了。