title: Android TelephonyManager dual sim
body: <p>I've read and tried a lot of solution about to detect if a mobile phone is a dual sim, but every solution doesn't work.</p>
  
  <p>I've tried with this code:</p>
  
  <pre><code>import java.lang.reflect.Method;
  import java.util.ArrayList;
  import java.util.List;
  
  import android.content.Context;
  import android.telephony.TelephonyManager;
  
  
  public final class TelephonyInfoNew {
  
      private static TelephonyInfoNew telephonyInfo;
      private String imsiSIM1;
      private String imsiSIM2;
      private boolean isSIM1Ready;
      private boolean isSIM2Ready;
  
      public String getImsiSIM1() {
          return imsiSIM1;
      }
  
      /*public static void setImsiSIM1(String imsiSIM1) {
          TelephonyInfo.imsiSIM1 = imsiSIM1;
      }*/
  
      public String getImsiSIM2() {
          return imsiSIM2;
      }
  
      /*public static void setImsiSIM2(String imsiSIM2) {
          TelephonyInfo.imsiSIM2 = imsiSIM2;
      }*/
  
      public boolean isSIM1Ready() {
          return isSIM1Ready;
      }
  
      /*public static void setSIM1Ready(boolean isSIM1Ready) {
          TelephonyInfo.isSIM1Ready = isSIM1Ready;
      }*/
  
      public boolean isSIM2Ready() {
          return isSIM2Ready;
      }
  
      /*public static void setSIM2Ready(boolean isSIM2Ready) {
          TelephonyInfo.isSIM2Ready = isSIM2Ready;
      }*/
  
      public boolean isDualSIM() {
          return imsiSIM2 != null;
      }
  
      private TelephonyInfoNew() {
      }
  
      public static TelephonyInfoNew getInstance(Context context){
  
          if(telephonyInfo == null) {
  
              telephonyInfo = new TelephonyInfoNew();
  
              TelephonyManager telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
  
              telephonyInfo.imsiSIM1 = telephonyManager.getDeviceId();
              telephonyInfo.imsiSIM2 = null;
  
              List&lt;String&gt; list = printTelephonyManagerMethodNamesForThisDeviceList(context);
              for (int i = 0; i &lt; list.size(); i++)
              {
                  try
                  {
                      System.out.println("1 -&gt; " + list.get(i));
                      telephonyInfo.imsiSIM1 = getDeviceIdBySlot(context, list.get(i), 0);
                      telephonyInfo.imsiSIM2 = getDeviceIdBySlot(context, list.get(i), 1);
                      break;
                  }catch(GeminiMethodNotFoundException e2)
                  {
                      e2.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
  
                      List&lt;String&gt; list1 = printTelephonyManagerMethodNamesForThisDeviceList(context);
                      for (int i = 0; i &lt; list1.size(); i++)
                      {
                          try
                          {
                              telephonyInfo.isSIM1Ready = getSIMStateBySlot(context, list1.get(i), 0);
                              telephonyInfo.isSIM2Ready = getSIMStateBySlot(context, list1.get(i), 1);
                              break;
                          }catch(GeminiMethodNotFoundException e2)
                          {
                              e2.printStackTrace();
                          }
                      }
                      e1.printStackTrace();
                  }
              }
          }
  
          return telephonyInfo;
      }
  
      private static String getDeviceIdBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {
  
          String imsi = null;
  
          TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  
          try{
  
              Class&lt;?&gt; telephonyClass = Class.forName(telephony.getClass().getName());
  
              Class&lt;?&gt;[] 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){
                  imsi = ob_phone.toString();
  
              }
          } catch (Exception e) {
              e.printStackTrace();
              throw new GeminiMethodNotFoundException(predictedMethodName);
          }
  
          return imsi;
      }
  
      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&lt;?&gt; telephonyClass = Class.forName(telephony.getClass().getName());
  
              Class&lt;?&gt;[] 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);
          }
      }
  
      public static List&lt;String&gt; printTelephonyManagerMethodNamesForThisDeviceList(Context context) {
  
          List&lt;String&gt; list = new ArrayList&lt;String&gt;();
  
          TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
          Class&lt;?&gt; telephonyClass;
          try {
              telephonyClass = Class.forName(telephony.getClass().getName());
              Method[] methods = telephonyClass.getMethods();
              for (int idx = 0; idx &lt; methods.length; idx++) {
                   //Log.println (Log.DEBUG, "Dual", methods[idx] + " declared by " + methods[idx].getDeclaringClass() + "\n");
                  String mth = methods[idx].getName();
                  //System.out.println(mth + " - " + methods[idx].getName() + " - ");
                  //String[] exp = mth.split(".");
                  //int len = exp.length;
                  //System.out.println(len);
                  //System.out.println(exp[len-1]);
                  list.add(mth);
  
                  //System.out.println("\n" + methods[idx] + " declared by " + methods[idx].getDeclaringClass());
              }
          } catch (ClassNotFoundException e) {
              e.printStackTrace();
          }
  
          return list;
      } 
  }
  </code></pre>
  
  <p>and on main:</p>
  
  <pre><code>TelephonyInfoNew telephonyInfo = TelephonyInfoNew.getInstance(context);
              String imsiSIM1 = telephonyInfo.getImsiSIM1();
              String imsiSIM2 = telephonyInfo.getImsiSIM2();
  
  System.out.println(" IMSI1 : " + imsiSIM1 + "\n" +
                      " IMSI2 : " + imsiSIM2 + "\n" +
                      " IS DUAL SIM : " + isDualSIM + "\n" +
                      " IS SIM1 READY : " + isSIM1Ready + "\n" +
                      " IS SIM2 READY : " + isSIM2Ready + "\n");
  </code></pre>
  
  <p>I receive always :</p>
  
  <pre><code>imsi1 = 0;
  imsi2 = 1;
  </code></pre>
  
  <p>Is possible to resolve the problem or detect if mobile phone is dual sim?</p>
  
  <p>Thanks a lot</p>