【记录】 Android 双卡手机获取两个IMEI等
2017-12-21 14:58
716 查看
1、前言
项目中遇到上传手机imei的问题。如果手机是双卡,目前只能获取默认的imei
[java] view
plain copy
TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
deviceId = mTelephonyMgr.getDeviceId();
2、解决办法
上网搜了很多代码终于找到一个有效的办法。
地址: 原创地址
3、防止原创地址不可用,现在贴代码
[java] view
plain copy
import java.lang.reflect.Method;
import android.content.Context;
1fff8
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 iNumeric1;//sim1 code number
private String iNumeric2;//sim2 code number
private boolean isSIM1Ready;//sim1
private boolean isSIM2Ready;//sim2
private String iDataConnected1 = "0";//sim1 0 no, 1 connecting, 2 connected, 3 suspended.
private String iDataConnected2 = "0";//sim2
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 boolean isSIM1Ready() {
return isSIM1Ready;
}
public boolean isSIM2Ready() {
return isSIM2Ready;
}
public boolean isDualSim(){
return imeiSIM2 != null;
}
public boolean isDataConnected1(){
if(TextUtils.equals(iDataConnected1, "2")||TextUtils.equals(iDataConnected1, "1"))
return true;
else
return false;
}
public boolean isDataConnected2(){
if(TextUtils.equals(iDataConnected2, "2")||TextUtils.equals(iDataConnected2, "1"))
return true;
else
return false;
}
public String getINumeric1(){
return iNumeric1;
}
public String getINumeric2(){
return iNumeric2;
}
public String getINumeric(){
if(imeiSIM2 != null){
if(iNumeric1 != null && iNumeric1.length() > 1)
return iNumeric1;
if(iNumeric2 != null && iNumeric2.length() > 1)
return iNumeric2;
}
return iNumeric1;
}
public void setCTelephoneInfo(){
TelephonyManager telephonyManager = ((TelephonyManager)
mContext.getSystemService(Context.TELEPHONY_SERVICE));
CTelephoneInfo.imeiSIM1 = telephonyManager.getDeviceId();
CTelephoneInfo.imeiSIM2 = null;
try {
CTelephoneInfo.imeiSIM1 = getOperatorBySlot(mContext, "getDeviceIdGemini", 0);
CTelephoneInfo.imeiSIM2 = getOperatorBySlot(mContext, "getDeviceIdGemini", 1);
CTelephoneInfo.iNumeric1 = getOperatorBySlot(mContext, "getSimOperatorGemini", 0);
CTelephoneInfo.iNumeric2 = getOperatorBySlot(mContext, "getSimOperatorGemini", 1);
CTelephoneInfo.iDataConnected1 = getOperatorBySlot(mContext, "getDataStateGemini", 0);
CTelephoneInfo.iDataConnected2 = getOperatorBySlot(mContext, "getDataStateGemini", 1);
} catch (GeminiMethodNotFoundException e) {
e.printStackTrace();
try {
CTelephoneInfo.imeiSIM1 = getOperatorBySlot(mContext, "getDeviceId", 0);
CTelephoneInfo.imeiSIM2 = getOperatorBySlot(mContext, "getDeviceId", 1);
CTelephoneInfo.iNumeric1 = getOperatorBySlot(mContext, "getSimOperator", 0);
CTelephoneInfo.iNumeric2 = getOperatorBySlot(mContext, "getSimOperator", 1);
CTelephoneInfo.iDataConnected1 = getOperatorBySlot(mContext, "getDataState", 0);
CTelephoneInfo.iDataConnected2 = getOperatorBySlot(mContext, "getDataState", 1);
} catch (GeminiMethodNotFoundException e1) {
//Call here for next manufacturer's predicted method name if you wish
e1.printStackTrace();
}
}
CTelephoneInfo.isSIM1Ready = telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY;
CTelephoneInfo.isSIM2Ready = false;
try {
CTelephoneInfo.isSIM1Ready = getSIMStateBySlot(mContext, "getSimStateGemini", 0);
CTelephoneInfo.isSIM2Ready = getSIMStateBySlot(mContext, "getSimStateGemini", 1);
} catch (GeminiMethodNotFoundException e) {
e.printStackTrace();
try {
CTelephoneInfo.isSIM1Ready = getSIMStateBySlot(mContext, "getSimState", 0);
CTelephoneInfo.isSIM2Ready = getSIMStateBySlot(mContext, "getSimState", 1);
} catch (GeminiMethodNotFoundException e1) {
//Call here for next manufacturer's predicted method name if you wish
e1.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 boolean getSIMStateBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {
boolean isReady = false;
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 getSimStateGemini = telephonyClass.getMethod(predictedMethodName, parameter);
Object[] obParameter = new Object[1];
obParameter[0] = slotID;
Object ob_phone = getSimStateGemini.invoke(telephony, obParameter);
if(ob_phone != null){
int simState = Integer.parseInt(ob_phone.toString());
if(simState == TelephonyManager.SIM_STATE_READY){
isReady = true;
}
}
} catch (Exception e) {
e.printStackTrace();
throw new GeminiMethodNotFoundException(predictedMethodName);
}
return isReady;
}
private static class GeminiMethodNotFoundException extends Exception {
/**
*
*/
private static final long serialVersionUID = -3241033488141442594L;
public GeminiMethodNotFoundException(String info) {
super(info);
}
}
}
项目中遇到上传手机imei的问题。如果手机是双卡,目前只能获取默认的imei
[java] view
plain copy
TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
deviceId = mTelephonyMgr.getDeviceId();
2、解决办法
上网搜了很多代码终于找到一个有效的办法。
地址: 原创地址
3、防止原创地址不可用,现在贴代码
[java] view
plain copy
import java.lang.reflect.Method;
import android.content.Context;
1fff8
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 iNumeric1;//sim1 code number
private String iNumeric2;//sim2 code number
private boolean isSIM1Ready;//sim1
private boolean isSIM2Ready;//sim2
private String iDataConnected1 = "0";//sim1 0 no, 1 connecting, 2 connected, 3 suspended.
private String iDataConnected2 = "0";//sim2
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 boolean isSIM1Ready() {
return isSIM1Ready;
}
public boolean isSIM2Ready() {
return isSIM2Ready;
}
public boolean isDualSim(){
return imeiSIM2 != null;
}
public boolean isDataConnected1(){
if(TextUtils.equals(iDataConnected1, "2")||TextUtils.equals(iDataConnected1, "1"))
return true;
else
return false;
}
public boolean isDataConnected2(){
if(TextUtils.equals(iDataConnected2, "2")||TextUtils.equals(iDataConnected2, "1"))
return true;
else
return false;
}
public String getINumeric1(){
return iNumeric1;
}
public String getINumeric2(){
return iNumeric2;
}
public String getINumeric(){
if(imeiSIM2 != null){
if(iNumeric1 != null && iNumeric1.length() > 1)
return iNumeric1;
if(iNumeric2 != null && iNumeric2.length() > 1)
return iNumeric2;
}
return iNumeric1;
}
public void setCTelephoneInfo(){
TelephonyManager telephonyManager = ((TelephonyManager)
mContext.getSystemService(Context.TELEPHONY_SERVICE));
CTelephoneInfo.imeiSIM1 = telephonyManager.getDeviceId();
CTelephoneInfo.imeiSIM2 = null;
try {
CTelephoneInfo.imeiSIM1 = getOperatorBySlot(mContext, "getDeviceIdGemini", 0);
CTelephoneInfo.imeiSIM2 = getOperatorBySlot(mContext, "getDeviceIdGemini", 1);
CTelephoneInfo.iNumeric1 = getOperatorBySlot(mContext, "getSimOperatorGemini", 0);
CTelephoneInfo.iNumeric2 = getOperatorBySlot(mContext, "getSimOperatorGemini", 1);
CTelephoneInfo.iDataConnected1 = getOperatorBySlot(mContext, "getDataStateGemini", 0);
CTelephoneInfo.iDataConnected2 = getOperatorBySlot(mContext, "getDataStateGemini", 1);
} catch (GeminiMethodNotFoundException e) {
e.printStackTrace();
try {
CTelephoneInfo.imeiSIM1 = getOperatorBySlot(mContext, "getDeviceId", 0);
CTelephoneInfo.imeiSIM2 = getOperatorBySlot(mContext, "getDeviceId", 1);
CTelephoneInfo.iNumeric1 = getOperatorBySlot(mContext, "getSimOperator", 0);
CTelephoneInfo.iNumeric2 = getOperatorBySlot(mContext, "getSimOperator", 1);
CTelephoneInfo.iDataConnected1 = getOperatorBySlot(mContext, "getDataState", 0);
CTelephoneInfo.iDataConnected2 = getOperatorBySlot(mContext, "getDataState", 1);
} catch (GeminiMethodNotFoundException e1) {
//Call here for next manufacturer's predicted method name if you wish
e1.printStackTrace();
}
}
CTelephoneInfo.isSIM1Ready = telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY;
CTelephoneInfo.isSIM2Ready = false;
try {
CTelephoneInfo.isSIM1Ready = getSIMStateBySlot(mContext, "getSimStateGemini", 0);
CTelephoneInfo.isSIM2Ready = getSIMStateBySlot(mContext, "getSimStateGemini", 1);
} catch (GeminiMethodNotFoundException e) {
e.printStackTrace();
try {
CTelephoneInfo.isSIM1Ready = getSIMStateBySlot(mContext, "getSimState", 0);
CTelephoneInfo.isSIM2Ready = getSIMStateBySlot(mContext, "getSimState", 1);
} catch (GeminiMethodNotFoundException e1) {
//Call here for next manufacturer's predicted method name if you wish
e1.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 boolean getSIMStateBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {
boolean isReady = false;
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 getSimStateGemini = telephonyClass.getMethod(predictedMethodName, parameter);
Object[] obParameter = new Object[1];
obParameter[0] = slotID;
Object ob_phone = getSimStateGemini.invoke(telephony, obParameter);
if(ob_phone != null){
int simState = Integer.parseInt(ob_phone.toString());
if(simState == TelephonyManager.SIM_STATE_READY){
isReady = true;
}
}
} catch (Exception e) {
e.printStackTrace();
throw new GeminiMethodNotFoundException(predictedMethodName);
}
return isReady;
}
private static class GeminiMethodNotFoundException extends Exception {
/**
*
*/
private static final long serialVersionUID = -3241033488141442594L;
public GeminiMethodNotFoundException(String info) {
super(info);
}
}
}
相关文章推荐
- 【记录】 Android 双卡手机获取IMEI等
- 【记录】 Android 双卡手机获取IMEI等
- 【记录】 Android 双卡手机获取IMEI等
- 关于android各种双卡手机获取imei,imsi的处置(mtk,展讯,高通等)
- 关于android各种双卡手机获取imei,imsi的处理(mtk,展讯,高通等)
- Android 获取双卡手机IMEI,IMSI,ICCID
- 关于android各种双卡手机获取imei,imsi的处置(mtk,展讯,高通等)
- 关于android各种双卡手机获取imei,imsi的处理(mtk,展讯,高通等)
- android 获取 imei号码 和 获取手机型号和系统版本号(未验证)
- Android编程获取手机的IMEI
- Android 获取imei号码,获取手机型号和系统版本号等信息
- android平台获取手机IMSI,IMEI ,序列号,和 手机号的方法
- Android开发之获取手机通话记录
- Android获取手机设备识别码(IMEI)和手机号码
- android系统如何获取imei号码,获取手机型号和系统版本号
- Android 笔记:获取本机手机号(适用于双卡双待手机)
- android使用TelephonyManager获取imei和其他手机信息
- 获取手机(ios,android)的设备唯一码(mac地址, IMEI)
- 获取双卡手机的两个卡的IMSI
- android 获取手机上面的IMSI序列号,IMEI,MAC地址,IP地址,当前时间,当前经纬度,获取网络类型