Is it possible to identify currently attached mobile network and network operators programmatically on Android? If it is possible please provide code samples
Thank you.
Is it possible to identify currently attached mobile network and network operators programmatically on Android? If it is possible please provide code samples
Thank you.
Try this,
// 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;
Required Permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
You can try this:
public String getNetworkOperatorName(){
TelephonyManager tm =
(TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE);
return(tm.getNetworkOperatorName());
}
You can use this for more methods
To get more details about networks:
// 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;
Never used it myself, but take a look at 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;
Required Permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
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;
}