2014-03-27 43 views
4

你好的朋友在我的應用我想在雙卡雙待手機,以顯示用戶自己的SIM卡的電話號碼但沒有給出任何類型的數字我還是谷歌搜索,但沒有得到exact.please電話號碼幫助我從雙卡獲取電話號碼。 注意以下情況不工作的雙卡拿到號碼:的Android如何獲得從雙卡手機

 String phonenumber = telephonymanager.getLine1Number(); 
+0

<使用權限android:name =「android.permission.READ_PHONE_STATE」/>你給這個嗎? – Sree

+0

雅已經給 –

回答

2

telephonymanager.getLine1Number()不保證返回的SIM卡號碼,如電話號碼並非實際存儲在所有的SIM卡。

更好的選擇是向用戶詢問一次電話號碼,並通過向該號碼發送消息來確認。

Here是同一個更好的解釋。

1

使用以下代碼。

使類如下SimNoInfo。

import java.lang.reflect.Method; 

import android.content.Context; 
import android.telephony.TelephonyManager; 

public final class SimNoInfo { 

private static SimNoInfo telephonyInfo; 
private String imeiSIM1; 
private String imeiSIM2; 
private boolean isSIM1Ready; 
private boolean isSIM2Ready; 

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; 
} 

private TelephonyInfo() { 
} 

public static SimNoInfo getInstance(Context context){ 

    if(telephonyInfo == null) { 

     telephonyInfo = new SimNoInfo(); 

     TelephonyManager telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)); 

     telephonyInfo.imeiSIM1 = telephonyManager.getDeviceId();; 
     telephonyInfo.imeiSIM2 = null; 

     try { 
      telephonyInfo.imeiSIM1 = getDeviceIdBySlot(context, "getDeviceIdGemini", 0); 
      telephonyInfo.imeiSIM2 = getDeviceIdBySlot(context, "getDeviceIdGemini", 1); 
     } catch (GeminiMethodNotFoundException e) { 
      e.printStackTrace(); 

      try { 
       telephonyInfo.imeiSIM1 = getDeviceIdBySlot(context, "getDeviceId", 0); 
       telephonyInfo.imeiSIM2 = getDeviceIdBySlot(context, "getDeviceId", 1); 
      } catch (GeminiMethodNotFoundException e1) { 
       //Call here for next manufacturer's predicted method name if you wish 
       e1.printStackTrace(); 
      } 
     } 

     telephonyInfo.isSIM1Ready = telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY; 
     telephonyInfo.isSIM2Ready = false; 

     try { 
      telephonyInfo.isSIM1Ready = getSIMStateBySlot(context, "getSimStateGemini", 0); 
      telephonyInfo.isSIM2Ready = getSIMStateBySlot(context, "getSimStateGemini", 1); 
     } catch (GeminiMethodNotFoundException e) { 

      e.printStackTrace(); 

      try { 
       telephonyInfo.isSIM1Ready = getSIMStateBySlot(context, "getSimState", 0); 
       telephonyInfo.isSIM2Ready = getSIMStateBySlot(context, "getSimState", 1); 
      } catch (GeminiMethodNotFoundException e1) { 
       //Call here for next manufacturer's predicted method name if you wish 
       e1.printStackTrace(); 
      } 
     } 
    } 

    return telephonyInfo; 
} 

private static String getDeviceIdBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException { 

    String imei = 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){ 
      imei = ob_phone.toString(); 

     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
     throw new GeminiMethodNotFoundException(predictedMethodName); 
    } 

    return imei; 
} 

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 = -996812356902545308L; 

    public GeminiMethodNotFoundException(String info) { 
     super(info); 
    } 
} 
} 

然後再使用這個類這樣的:

private void isDualSimOrNot(){ 
    SimNoInfo telephonyInfo = SimNoInfo.getInstance(this); 

    String imeiSIM1 = telephonyInfo.getImeiSIM1(); 
    String imeiSIM2 = telephonyInfo.getImeiSIM2(); 

    boolean isSIM1Ready = telephonyInfo.isSIM1Ready(); 
    boolean isSIM2Ready = telephonyInfo.isSIM2Ready(); 

    boolean isDualSIM = telephonyInfo.isDualSIM(); 
    Log.i("Dual = "," IME1 : " + imeiSIM1 + "\n" + 
      " IME2 : " + imeiSIM2 + "\n" + 
      " IS DUAL SIM : " + isDualSIM + "\n" + 
      " IS SIM1 READY : " + isSIM1Ready + "\n" + 
      " IS SIM2 READY : " + isSIM2Ready + "\n"); 
} 

並且不要忘了在androidmanifest添加權限。

<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
+3

從上面的代碼我得到手機是雙卡與否或兩個SIM卡的IMEI號碼,但不能同時獲得SIM卡號碼 –

+1

它不會工作,來源是從這個鏈接:http://stackoverflow.com/questions/14517338/Android系統的檢查,是否最電話是-雙SIM –

+1

答案被複制的:http://stackoverflow.com/a/17499889/2798289 – Govind

相關問題