相信关于这个获取网上有很多例子。我说说的情况吧,我项目使用的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作为唯一码那就这样了。
最后
以上就是平淡猎豹最近收集整理的关于android 读取 IMEI 和 MEID 的处理的全部内容,更多相关android内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复