To uniquely identify a mobile subscriber network, the MCC combined with a Mobile Network Code (MNC). The combination of MCC and MNC is called HNI (Home network identity) and is the combination of both in one string (e.g., MCC= 262 and MNC = 01 results in an HNI of 26201).
The MCC is the mobile country code and the MNC is the mobile network code which together uniquely identify a mobile network operator.
Mobile Country Code (MCC) is defined by ITU-T as standard E. 212. MCC is a mobile code consisting of three digits used to identify GSM networks. MCC is also used along with the International Mobile Subscriber Identity (IMSI) to identify the region from which mobile subscriber belongs.
The TelephonyManager has a method to return the MCC+MNC as a String (getNetworkOperator()) which will do you what you want. You can get access it via:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String networkOperator = tel.getNetworkOperator();
if (!TextUtils.isEmpty(networkOperator)) {
int mcc = Integer.parseInt(networkOperator.substring(0, 3));
int mnc = Integer.parseInt(networkOperator.substring(3));
}
}
You do know there are two MCC/MNC's for an active phone? (One is the country code and carrier id for the Sim card, the other is for the network/cell tower in use.)
If the getResources().getConfiguration().mcc
is not empty in airplane mode, it's the Sim
value TelephonyManager.getSimOperator()
, not the tower
value TelephonyManager.getNetworkOperator()
.
I don't know which the OP wants, but Answer 3 will give him different results than his original code if the getConfiguration
is really the Sim
values.
getResources().getConfiguration().mcc
is a bad choice because it returns an integer, hence compromising valid values such as 01
, or 044
. Clearly integer is not a good option for this.
See details in Mobile_Network_Code
Update: in Australia, we verified a wrong case here. The getNetworkOperator
returns different value from getSimOperator
, where the latter is correct.
See details in Android doc: TelephonyManager
You can access the current configuration by getResources().getConfiguration()
does the trick.
Okay, it turns out that the getResources().getConfiguration().mcc trick is likely better for most purposes, because with the other one if the person puts their phone in airplane mode or otherwise uses Wi-Fi, then it returns an empty MCC.
I found out that network operator sometimes can be like 65@5
when not connected to the operator (service unavailable) even if there is a a SIM card inserted. This happened on Samsung S2 running Android 4.1.2.
So you have to be careful when converting to Int.
int mcc = Integer.parseInt(networkOperator.substring(0, 3));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With