Android – Figuring out the Mobile Network programmatically

On building an Android app for a specific mobile operator, I was once faced with the problem of making sure the phone running the app would have a SIM connected to that particular operator. Since the app I built functions completely independently from the mobile operator, I needed to figure out a solution to stop users on other networks from using it to ensure exclusivity for my client.

The obvious solution to this (at least, the solution which I thought of first) is to check the SIM’s mobile number, do some parsing and then figure out which operator the SIM is on. However, turns out nowadays the mobile phone number is not stored on the SIM card like it was in the old days. It is assigned dynamically by the mobile service provider, and this is why when you lose your SIM card you can simply get a new one with the old number assigned to it.

The method I used to get this done is using the checking the Mobile Country Code (MCC) + Mobile Network Code (MNC) combination that can be gained through a call to the TelephonyManager class’s getSimOperator method.

Code is as follows:

import android.telephony.TelephonyManager;
  TelephonyManager tMgr =  (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  String operator = tMgr.getSimOperator(); // this returns the MCC+MNC
/* Do whatever checks you need here */

The above code will return the MCC + MNC composite for you. For Sri Lanka, the MCC is 413 and the MNCs are as follows:

  • Mobitel – 01
  • Dialog – 02
  • Etisalat – 03
  • Airtel – 05
  • Hutch – 08

For more information on MCCs and MNCs, you can go to the Wikipedia article.