4

是否可以在 Android 上以编程方式识别当前连接的移动网络和网络运营商?如果可能,请提供代码示例

谢谢你。

4

4 回答 4

13

尝试这个,

// Get System TELEPHONY service reference
TelephonyManager tManager = (TelephonyManager) getBaseContext()
      .getSystemService(Context.TELEPHONY_SERVICE);

// Get carrier name (Network Operator Name)
String carrierName = tManager.getNetworkOperatorName();

// Get Phone model and manufacturer name
 String manufacturer = Build.MANUFACTURER;
 String model = Build.MODEL;

所需权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
于 2015-04-29T08:39:44.627 回答
2

你可以试试这个:

  public String getNetworkOperatorName(){
        TelephonyManager tm =
            (TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE);
        return(tm.getNetworkOperatorName());
    } 

您可以将其用于更多方法

要获取有关网络的更多详细信息:

// Get the connected network operator ID (MCC + MNC)
String networkOperatorId = telephonyManager.getNetworkOperator();

// Get the connected network operator name
String networkName = telephonyManager.getNetworkOperatorName();

// Get the type of network you are connected with 
int networkType = telephonyManager.getNetworkType();
switch (networkType) {
case (TelephonyManager.NETWORK_TYPE_1xRTT) :" Block of Code ":
break;
case (TelephonyManager.NETWORK_TYPE_CDMA) :"  Block of Code ":
break;
case (TelephonyManager.NETWORK_TYPE_EDGE) : "  Block of Code ":
break;
case (TelephonyManager.NETWORK_TYPE_EVDO_0) :"  Block of Code ":
break;
于 2015-04-29T08:44:28.967 回答
1

我自己从来没有用过,但是看看 TelephonyManager->

// Get System TELEPHONY service reference
    TelephonyManager tManager = (TelephonyManager) getBaseContext()
          .getSystemService(Context.TELEPHONY_SERVICE);

    // Get Mobile No 

    String mPhoneNumber = tManager.getLine1Number();

    // Get carrier name (Network Operator Name)
    String carrierName = tManager.getNetworkOperatorName();

    // Get Phone model and manufacturer name
     String manufacturer = Build.MANUFACTURER;
     String model = Build.MODEL;

所需权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
于 2015-04-29T08:45:31.790 回答
0
private static String getOutput(Context context, String methodName, int slotId) {
    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    Class<?> telephonyClass;
    String reflectionMethod = null;
    String output = "";
    try {
        telephonyClass = Class.forName(telephony.getClass().getName());
        for (Method method : telephonyClass.getMethods()) {
            String name = method.getName();
            if (name.contains(methodName)) {
                Class<?>[] params = method.getParameterTypes();
                if (params.length == 1 && params[0].getName().equals("int")) {
                    reflectionMethod = name;
                }
            }
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    if (reflectionMethod != null) {
        try {
            output = getOpByReflection(telephony, reflectionMethod, slotId, false);
            return output;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return output;
}

  private static String getOpByReflection(TelephonyManager telephony, String predictedMethodName, int slotID, boolean isPrivate) {

    Log.i("Reflection", "Method: " + predictedMethodName+" "+slotID);
    String result = null;

    try {

        Class<?> telephonyClass = Class.forName(telephony.getClass().getName());

        Class<?>[] parameter = new Class[1];
        parameter[0] = int.class;
        Method getSimID;
        if (slotID != -1) {
            if (isPrivate) {
                getSimID = telephonyClass.getDeclaredMethod(predictedMethodName, parameter);
            } else {
                getSimID = telephonyClass.getMethod(predictedMethodName, parameter);
            }
        } else {
            if (isPrivate) {
                getSimID = telephonyClass.getDeclaredMethod(predictedMethodName);
            } else {
                getSimID = telephonyClass.getMethod(predictedMethodName);
            }
        }

        Object ob_phone;
        Object[] obParameter = new Object[1];
        obParameter[0] = slotID;
        if (getSimID != null) {
            if (slotID != -1) {
                ob_phone = getSimID.invoke(telephony, obParameter);
            } else {
                ob_phone = getSimID.invoke(telephony);
            }

            if (ob_phone != null) {
                result = ob_phone.toString();

            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    Log.i("Reflection", "Result: " + result);
    return result;
}
于 2018-10-24T01:04:29.773 回答