是否有可能在Android上以编程方式识别当前连接的移动网络和网络运营商? 如果可能,请提供代码示例
谢谢。
答案 0 :(得分:11)
试试这个,
// 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"/>
答案 1 :(得分:1)
你可以试试这个:
public String getNetworkOperatorName(){
TelephonyManager tm =
(TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE);
return(tm.getNetworkOperatorName());
}
您可以将其用于更多methods
要获取有关网络的更多详细信息:
// 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;
答案 2 :(得分: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"/>
答案 3 :(得分: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;
}