I'm trying to get the mcc
and mnc
after a SIM LOADED
state, in order to check if the SIM card did change without READ PHONE STATE
permissions, in order to disable the app requests for some networks and in some countries that the user do not want.
Since getSimOperator()
may change according to the current carrier (e.g. when the user is on roaming or not) I decided to use the getNetworkOperator()
.
Although this method can return null
even if the SIM
is LOADED
and may return different results e.g. a lycamobile card with GSM only connection is giving me mnc = 01
and when I take the SIM card out and put it again it gives me mnc = 04
.
Does some one know why the mnc gives different results for the getNetworkOperator()
? Which method is better, getNetworkOperator()
or getSimOperator()
for this case?
Also, I cannot use getResources().getConfiguration().mcc
because it gives a int number which might remove the 0
before e.g. gives 4
instead of 04
.
This is my code to check a SIM state change:
@Override
public void onReceive(final Context context, Intent intent) {
if (intent != null) {
Bundle extras = intent.getExtras();
if (extras != null) {
String ss = extras.getString(EXTRAS_SIM_STATUS);
if (ss != null && (ss.equals("LOADED"))) {
TelephonyManager telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && hasPermissions(READ_PHONE_STATE)) {
//here I get the imsi
}else{
L.d("NetworkOperator result %s", telephonyManager.getNetworkOperator());
//saving in shared preferences in order to check if the sim is allowed or not
//this is also called on application onCreate() so I can check the current SIM
}
}
}
}
}
PS: the SIM card that I'm using only has GSM connection. Also I tried with an other card (with 4g capabilities) and everything works as expected the mnc
is the same 01
for a vodafone card.
Since getSimOperator() may change according to the current carrier (e.g. when the user is on roaming or not) I decided to use the getNetworkOperator().
backwards. getSimOperator should change with the SIM -- but not when roaming, etc. – Wilbourn