2017-04-25 72 views

回答

0

我已經找到一個posible解決方案。我已經使用了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)。 如果有人有其他選擇,歡迎。

0

在Android Android 5.1(API22)之前沒有API。但那麼你有SubscriptionManager和它的getActiveSubscriptionInfoList()

+0

感謝您的回答,但與SubscriptionManager的問題是,不是每個SIM卡的返回網絡,或者如果它真的返回它,我不知道如何得到它 – efr