Search code examples
androiddual-sim

Get both networks when a phone has Dual Sim


I am trying to get a list of networks on Android devices that have multiple SIM cards "dual sim."

I use the TelephonyManager class but the method getNetworkType only returns the network for the first sim "sim 1."


Solution

  • I have found a posible solution. I have used the android reflection to call TelephonyManager methods for example if i want the data Network I can use getDataNetworkType as follows:

    getNetworkTypeReflection(telephonyManager, "getDataNetworkType", slot, false);

    private static String getNetworkTypeReflection(final TelephonyManager telephony, final String predictedMethodName, final int slotID, final boolean isPrivate) {
    
            String result = null;
    
            try {
    
                final Class<?> telephonyClass = Class.forName(telephony.getClass().getName());
    
                final Class<?>[] parameter = new Class[1];
                parameter[0] = int.class;
                final Method getSubtecnology;
                if (slotID != -1) {
                    if (isPrivate) {
                        getSubtecnology = telephonyClass.getDeclaredMethod(predictedMethodName, parameter);
                    } else {
                        getSubtecnology = telephonyClass.getMethod(predictedMethodName, parameter);
                    }
                } else {
                    if (isPrivate) {
                        getSubtecnology = telephonyClass.getDeclaredMethod(predictedMethodName);
                    } else {
                        getSubtecnology = telephonyClass.getMethod(predictedMethodName);
                    }
                }
    
                final Object obPhone;
                final Object[] obParameter = new Object[1];
                obParameter[0] = slotID;
                if (getSubtecnology != null) {
                    if (slotID != -1) {
                        obPhone = getSubtecnology.invoke(telephony, obParameter);
                    } else {
                        obPhone = getSubtecnology.invoke(telephony);
                    }
    
                    if (obPhone != null) {
                        result = obPhone.toString();
    
                    }
                }
            } catch (Exception e) {
                //e.printStackTrace();
                return null;
            }
            return result;
        }
    

    The problem is that this option only works on Android 5.1 (API22) but only in some device in others you need Android 7.0 (API24). If anyone has other options are welcome.