how do you get the phone's MCC and MNC in Android?

JavaAndroidMobile

Java Problem Overview


The only way I've found of retrieving MCC and MNC is by overriding an activity's onConfigurationChanged method, as such:

public void onConfigurationChanged(Configuration config)
{
	super.onConfigurationChanged(config);
	DeviceData.MCC = "" + config.mcc;
	DeviceData.MNC = ""  +config.mnc;
}

However, I need this data as soon as the app starts and can't wait for the user to switch the phone's orientation or equivalent to trigger this method. Is there a better way to access the current Configuration object?

Java Solutions


Solution 1 - Java

The [TelephonyManager][1] has a method to return the MCC+MNC as a String ([getNetworkOperator()][2]) 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));
    }
}

[1]: http://developer.android.com/reference/android/telephony/TelephonyManager.html "android.telephony.TelephonyManager" [2]: http://developer.android.com/reference/android/telephony/TelephonyManager.html#getNetworkOperator() "getNetworkOperator()"

Solution 2 - Java

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.

Solution 3 - Java

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

Solution 4 - Java

You can access the current configuration by getResources().getConfiguration() does the trick.

Solution 5 - Java

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.

Solution 6 - Java

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.

enter image description here

So you have to be careful when converting to Int.

 int mcc = Integer.parseInt(networkOperator.substring(0, 3));

Solution 7 - Java

this is updated. use this

 TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String networkOperator = tel.getSimOperator();
    System.out.println("************mnc,mcc"+networkOperator);
    if (!TextUtils.isEmpty(networkOperator)) {
         mcc = networkOperator.substring(0, 3);
       mnc = networkOperator.substring(3);System.out.println("************mnc,mcc"+mnc+mcc);
    }mnc_mcc.setText("************mnc,mcc"+mnc+","+mcc);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser108078View Question on Stackoverflow
Solution 1 - JavajargonjustinView Answer on Stackoverflow
Solution 2 - JavadanSView Answer on Stackoverflow
Solution 3 - Javabruno.bragaView Answer on Stackoverflow
Solution 4 - Javauser188128View Answer on Stackoverflow
Solution 5 - JavaArtemView Answer on Stackoverflow
Solution 6 - JavakakopappaView Answer on Stackoverflow
Solution 7 - JavaAkash pasupathiView Answer on Stackoverflow