Android : Check whether the phone is dual SIM

前端 未结 8 1975
轻奢々
轻奢々 2020-11-22 01:38

After a lot of research on forums, now I know that there is no way to find IMSI or SIM serial number for both the SIM cards in a dual SIM phone (except for contacting the ma

8条回答
  •  梦毁少年i
    2020-11-22 01:55

    I have a Samsung Duos device with Android 4.4.4 and the method suggested by Seetha in the accepted answer (i.e. call getDeviceIdDs) does not work for me, as the method does not exist. I was able to recover all the information I needed by calling method "getDefault(int slotID)", as shown below:

    public static void samsungTwoSims(Context context) {
        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 getFirstMethod = telephonyClass.getMethod("getDefault", parameter);
    
            Log.d(TAG, getFirstMethod.toString());
    
            Object[] obParameter = new Object[1];
            obParameter[0] = 0;
            TelephonyManager first = (TelephonyManager) getFirstMethod.invoke(null, obParameter);
    
            Log.d(TAG, "Device Id: " + first.getDeviceId() + ", device status: " + first.getSimState() + ", operator: " + first.getNetworkOperator() + "/" + first.getNetworkOperatorName());
    
            obParameter[0] = 1;
            TelephonyManager second = (TelephonyManager) getFirstMethod.invoke(null, obParameter);
    
            Log.d(TAG, "Device Id: " + second.getDeviceId() + ", device status: " + second.getSimState()+ ", operator: " + second.getNetworkOperator() + "/" + second.getNetworkOperatorName());
        } catch (Exception e) {
            e.printStackTrace();
        }   
    }
    

    Also, I rewrote the code that iteratively tests for methods to recover this information so that it uses an array of method names instead of a sequence of try/catch. For instance, to determine if we have two active SIMs we could do:

    private static String[] simStatusMethodNames = {"getSimStateGemini", "getSimState"};
    
    
    public static boolean hasTwoActiveSims(Context context) {
        boolean first = false, second = false;
    
        for (String methodName: simStatusMethodNames) {
            // try with sim 0 first
            try {
                first = getSIMStateBySlot(context, methodName, 0);
                // no exception thrown, means method exists
                second = getSIMStateBySlot(context, methodName, 1);
               return first && second;
            } catch (GeminiMethodNotFoundException e) {
                // method does not exist, nothing to do but test the next
            }
        }
        return false;
    }
    

    This way, if a new method name is suggested for some device, you can simply add it to the array and it should work.

提交回复
热议问题