【记录】 Android 双卡手机获取两个IMEI等

来源:互联网 发布:骑马与砍杀 知乎 编辑:程序博客网 时间:2024/05/16 12:02

1、前言

项目中遇到上传手机imei的问题。如果手机是双卡,目前只能获取默认的imei

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);     
  2. deviceId = mTelephonyMgr.getDeviceId();  


2、解决办法

上网搜了很多代码终于找到一个有效的办法。

地址:  原创地址


3、防止原创地址不可用,现在贴代码


[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. import java.lang.reflect.Method;  
  2. import android.content.Context;  
  3. import android.telephony.TelephonyManager;  
  4. import android.text.TextUtils;  
  5.   
  6. public class CTelephoneInfo {  
  7.     private static final String TAG = CTelephoneInfo.class.getSimpleName();  
  8.     private String imeiSIM1;// IMEI  
  9.     private String imeiSIM2;//IMEI    
  10.     private String iNumeric1;//sim1 code number   
  11.     private String iNumeric2;//sim2 code number   
  12.     private boolean isSIM1Ready;//sim1  
  13.     private boolean isSIM2Ready;//sim2  
  14.     private String iDataConnected1 = "0";//sim1 0 no, 1 connecting, 2 connected, 3 suspended.  
  15.     private String iDataConnected2 = "0";//sim2  
  16.     private static CTelephoneInfo CTelephoneInfo;  
  17.     private static Context mContext;  
  18.       
  19.     private CTelephoneInfo() {  
  20.     }  
  21.   
  22.     public synchronized static CTelephoneInfo getInstance(Context context){  
  23.         if(CTelephoneInfo == null) {      
  24.             CTelephoneInfo = new CTelephoneInfo();  
  25.         }  
  26.         mContext = context;       
  27.         return CTelephoneInfo;  
  28.     }  
  29.       
  30.     public String getImeiSIM1() {  
  31.         return imeiSIM1;  
  32.     }  
  33.       
  34.     public String getImeiSIM2() {  
  35.         return imeiSIM2;  
  36.     }  
  37.     public boolean isSIM1Ready() {  
  38.         return isSIM1Ready;  
  39.     }  
  40.       
  41.     public boolean isSIM2Ready() {  
  42.         return isSIM2Ready;  
  43.     }  
  44.       
  45.     public boolean isDualSim(){  
  46.         return imeiSIM2 != null;  
  47.     }  
  48.       
  49.     public boolean isDataConnected1(){  
  50.         if(TextUtils.equals(iDataConnected1, "2")||TextUtils.equals(iDataConnected1, "1"))  
  51.             return true;  
  52.         else   
  53.             return false;  
  54.     }  
  55.       
  56.     public boolean isDataConnected2(){  
  57.         if(TextUtils.equals(iDataConnected2, "2")||TextUtils.equals(iDataConnected2, "1"))  
  58.             return true;  
  59.         else   
  60.             return false;  
  61.     }  
  62.       
  63.     public String getINumeric1(){  
  64.         return iNumeric1;  
  65.     }  
  66.       
  67.     public String getINumeric2(){  
  68.         return iNumeric2;  
  69.     }  
  70.       
  71.     public String getINumeric(){  
  72.         if(imeiSIM2 != null){  
  73.             if(iNumeric1 != null && iNumeric1.length() > 1)  
  74.                 return iNumeric1;  
  75.               
  76.             if(iNumeric2 != null && iNumeric2.length() > 1)  
  77.                 return iNumeric2;  
  78.         }         
  79.         return iNumeric1;  
  80.     }  
  81.       
  82.     public void setCTelephoneInfo(){  
  83.         TelephonyManager telephonyManager = ((TelephonyManager)   
  84.                 mContext.getSystemService(Context.TELEPHONY_SERVICE));    
  85.         CTelephoneInfo.imeiSIM1 = telephonyManager.getDeviceId();  
  86.         CTelephoneInfo.imeiSIM2 = null;   
  87.         try {  
  88.             CTelephoneInfo.imeiSIM1 = getOperatorBySlot(mContext, "getDeviceIdGemini"0);  
  89.             CTelephoneInfo.imeiSIM2 = getOperatorBySlot(mContext, "getDeviceIdGemini"1);  
  90.             CTelephoneInfo.iNumeric1 = getOperatorBySlot(mContext, "getSimOperatorGemini"0);  
  91.             CTelephoneInfo.iNumeric2 = getOperatorBySlot(mContext, "getSimOperatorGemini"1);  
  92.             CTelephoneInfo.iDataConnected1 = getOperatorBySlot(mContext, "getDataStateGemini"0);  
  93.             CTelephoneInfo.iDataConnected2 = getOperatorBySlot(mContext, "getDataStateGemini"1);  
  94.         } catch (GeminiMethodNotFoundException e) {  
  95.             e.printStackTrace();  
  96.             try {  
  97.                  CTelephoneInfo.imeiSIM1 = getOperatorBySlot(mContext, "getDeviceId"0);  
  98.                  CTelephoneInfo.imeiSIM2 = getOperatorBySlot(mContext, "getDeviceId"1);  
  99.                  CTelephoneInfo.iNumeric1 = getOperatorBySlot(mContext, "getSimOperator"0);  
  100.                  CTelephoneInfo.iNumeric2 = getOperatorBySlot(mContext, "getSimOperator"1);   
  101.                  CTelephoneInfo.iDataConnected1 = getOperatorBySlot(mContext, "getDataState"0);  
  102.                  CTelephoneInfo.iDataConnected2 = getOperatorBySlot(mContext, "getDataState"1);  
  103.             } catch (GeminiMethodNotFoundException e1) {  
  104.                 //Call here for next manufacturer's predicted method name if you wish  
  105.                 e1.printStackTrace();  
  106.             }  
  107.         }  
  108.         CTelephoneInfo.isSIM1Ready = telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY;  
  109.         CTelephoneInfo.isSIM2Ready = false;  
  110.   
  111.         try {  
  112.             CTelephoneInfo.isSIM1Ready = getSIMStateBySlot(mContext, "getSimStateGemini"0);  
  113.             CTelephoneInfo.isSIM2Ready = getSIMStateBySlot(mContext, "getSimStateGemini"1);  
  114.         } catch (GeminiMethodNotFoundException e) {  
  115.             e.printStackTrace();  
  116.             try {  
  117.                 CTelephoneInfo.isSIM1Ready = getSIMStateBySlot(mContext, "getSimState"0);  
  118.                 CTelephoneInfo.isSIM2Ready = getSIMStateBySlot(mContext, "getSimState"1);  
  119.             } catch (GeminiMethodNotFoundException e1) {  
  120.                 //Call here for next manufacturer's predicted method name if you wish  
  121.                 e1.printStackTrace();  
  122.             }  
  123.         }  
  124.     }  
  125.       
  126.     private static  String getOperatorBySlot(Context context, String predictedMethodName, int slotID)   
  127.              throws GeminiMethodNotFoundException {   
  128.         String inumeric = null;   
  129.         TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);      
  130.         try{      
  131.             Class<?> telephonyClass = Class.forName(telephony.getClass().getName());    
  132.             Class<?>[] parameter = new Class[1];  
  133.             parameter[0] = int.class;  
  134.             Method getSimID = telephonyClass.getMethod(predictedMethodName, parameter);               
  135.             Object[] obParameter = new Object[1];  
  136.             obParameter[0] = slotID;  
  137.             Object ob_phone = getSimID.invoke(telephony, obParameter);    
  138.             if(ob_phone != null){  
  139.                 inumeric = ob_phone.toString();   
  140.             }  
  141.         } catch (Exception e) {  
  142.             e.printStackTrace();  
  143.             throw new GeminiMethodNotFoundException(predictedMethodName);  
  144.         }     
  145.         return inumeric;  
  146.     }  
  147.       
  148.     private static  boolean getSIMStateBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {  
  149.           
  150.         boolean isReady = false;  
  151.       
  152.         TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);  
  153.       
  154.         try{  
  155.       
  156.             Class<?> telephonyClass = Class.forName(telephony.getClass().getName());  
  157.       
  158.             Class<?>[] parameter = new Class[1];  
  159.             parameter[0] = int.class;  
  160.             Method getSimStateGemini = telephonyClass.getMethod(predictedMethodName, parameter);  
  161.       
  162.             Object[] obParameter = new Object[1];  
  163.             obParameter[0] = slotID;  
  164.             Object ob_phone = getSimStateGemini.invoke(telephony, obParameter);  
  165.       
  166.             if(ob_phone != null){  
  167.                 int simState = Integer.parseInt(ob_phone.toString());  
  168.                 if(simState == TelephonyManager.SIM_STATE_READY){  
  169.                     isReady = true;  
  170.                 }  
  171.             }  
  172.         } catch (Exception e) {  
  173.             e.printStackTrace();  
  174.             throw new GeminiMethodNotFoundException(predictedMethodName);  
  175.         }  
  176.       
  177.         return isReady;  
  178.     }  
  179.       
  180.     private static class GeminiMethodNotFoundException extends Exception {    
  181.   
  182.         /** 
  183.          *  
  184.          */  
  185.         private static final long serialVersionUID = -3241033488141442594L;  
  186.   
  187.         public GeminiMethodNotFoundException(String info) {  
  188.             super(info);  
  189.         }  
  190.     }  
  191.       
  192. }  
阅读全文
0 0
原创粉丝点击