简体   繁体   English

当手机具有Dual 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." 我想在Android设备上获得一个网络列表,其中包含多个SIM卡“双卡双待”。

I use the TelephonyManager class but the method getNetworkType only returns the network for the first sim "sim 1." 我使用TelephonyManager类,但方法getNetworkType仅返回第一个sim“sim 1”的网络。

There's no API for this before Android Android 5.1 (API22). Android Android 5.1(API22)之前没有API。 But then you have SubscriptionManager and its getActiveSubscriptionInfoList() 但是你有SubscriptionManager及其getActiveSubscriptionInfoList()

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: 我已经使用android反射来调用TelephonyManager方法,例如,如果我想要数据网络我可以使用getDataNetworkType,如下所示:

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). 问题是此选项仅适用于Android 5.1(API22),但仅在其他设备中需要Android 7.0(API24)。 If anyone has other options are welcome. 如果有人有其他选择,欢迎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM