我正在尝试获取具有多个 SIM 卡“双卡”的 Android 设备上的网络列表。
我使用 TelephonyManager 类,但该方法getNetworkType
只返回第一个 sim“sim 1”的网络。
在 Android Android 5.1 (API22) 之前没有这方面的 API。但是你有SubscriptionManager
它的getActiveSubscriptionInfoList()
我找到了一个可行的解决方案。我已经使用 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;
}
问题是此选项仅适用于 Android 5.1 (API22),但仅适用于某些设备,而其他设备则需要 Android 7.0 (API24)。如果有人有其他选择,欢迎。