How to get Mobile Country Code like +91 for India?

Learn how to get mobile country code like +91 for india? with practical examples, diagrams, and best practices. Covers android, telephonymanager development techniques with visual explanations.

How to Get the Mobile Country Code (MCC) like +91 for India in Android

Hero image for How to get Mobile Country Code like +91 for India?

Learn how to programmatically retrieve the Mobile Country Code (MCC) and Mobile Network Code (MNC) in Android, essential for identifying a user's country and network.

Identifying a user's country based on their mobile network can be crucial for various applications, such as localization, regional content delivery, or fraud detection. In Android, the TelephonyManager class provides access to device telephony services, including network-related information like the Mobile Country Code (MCC) and Mobile Network Code (MNC). This article will guide you through the process of obtaining these codes and interpreting them to determine the user's country.

Understanding MCC and MNC

The Mobile Country Code (MCC) is a 3-digit number that uniquely identifies the country of a mobile subscriber. For example, India has an MCC of 404 and 405, while the United States has MCCs like 310, 311, and 312. The Mobile Network Code (MNC) is a 2 or 3-digit number that identifies the specific mobile network operator within that country. Together, MCC and MNC form the IMSI (International Mobile Subscriber Identity) which uniquely identifies a mobile subscriber.

To retrieve these codes, your Android application will need appropriate permissions to access telephony information. The TelephonyManager service is the gateway to this data.

Required Permissions

Before your application can access telephony information, you must declare the READ_PHONE_STATE permission in your AndroidManifest.xml file. This is a dangerous permission, meaning that on Android 6.0 (API level 23) and higher, you must also request this permission at runtime from the user.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <application
        ...
    </application>
</manifest>

Adding READ_PHONE_STATE permission to AndroidManifest.xml

Retrieving MCC and MNC Programmatically

Once the permissions are handled, you can use the TelephonyManager to get the network operator information. The getNetworkOperator() method returns a string that contains both the MCC and MNC. You then parse this string to extract the individual codes.

flowchart TD
    A[Start Application] --> B{Check READ_PHONE_STATE Permission?}
    B -- No --> C[Request Permission]
    C --> D{Permission Granted?}
    D -- No --> E[Handle Permission Denied]
    B -- Yes --> F[Get TelephonyManager Service]
    D -- Yes --> F
    F --> G[Call getNetworkOperator()]
    G --> H{Is networkOperator null or empty?}
    H -- Yes --> I[Handle No Network Info]
    H -- No --> J[Extract MCC (first 3 digits)]
    J --> K[Extract MNC (remaining digits)]
    K --> L[Use MCC/MNC for Country Lookup]
    L --> M[End]

Flowchart for retrieving MCC and MNC in Android

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class NetworkInfoHelper {

    public static final int PERMISSION_REQUEST_CODE = 101;

    public static String getMobileCountryCode(Context context) {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) 
            != PackageManager.PERMISSION_GRANTED) {
            // Permission not granted, request it or handle accordingly
            return null;
        }

        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (telephonyManager != null) {
            String networkOperator = telephonyManager.getNetworkOperator();
            if (networkOperator != null && networkOperator.length() >= 3) {
                return networkOperator.substring(0, 3);
            }
        }
        return null;
    }

    public static String getMobileNetworkCode(Context context) {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) 
            != PackageManager.PERMISSION_GRANTED) {
            // Permission not granted, request it or handle accordingly
            return null;
        }

        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (telephonyManager != null) {
            String networkOperator = telephonyManager.getNetworkOperator();
            if (networkOperator != null && networkOperator.length() >= 5) { // MCC (3) + MNC (2 or 3)
                return networkOperator.substring(3);
            }
        }
        return null;
    }

    // Example of how to request permission in an Activity
    public static void requestPhoneStatePermission(android.app.Activity activity) {
        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE) 
            != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity, 
                                            new String[]{Manifest.permission.READ_PHONE_STATE}, 
                                            PERMISSION_REQUEST_CODE);
        }
    }

    // Handle permission result in your Activity's onRequestPermissionsResult method
    /*
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission granted, you can now get MCC/MNC
                String mcc = NetworkInfoHelper.getMobileCountryCode(this);
                String mnc = NetworkInfoHelper.getMobileNetworkCode(this);
                Log.d("NetworkInfo", "MCC: " + mcc + ", MNC: " + mnc);
            } else {
                // Permission denied
                Log.w("NetworkInfo", "READ_PHONE_STATE permission denied.");
            }
        }
    }
    */
}

Java code to retrieve MCC and MNC with runtime permission handling

Mapping MCC to Country Name

Once you have the MCC, you can map it to a country name. There isn't a direct API in Android for this, so you'll typically need a lookup table. Many online resources provide lists of MCCs and their corresponding countries. You can embed this data in your app (e.g., as a JSON file or a database) or fetch it from an external API.

For example, if getMobileCountryCode() returns "404", you would look up "404" in your table and find that it corresponds to "India".

1. Add Permission

Declare READ_PHONE_STATE in your AndroidManifest.xml.

2. Request Runtime Permission

For Android 6.0+, request READ_PHONE_STATE permission from the user at runtime.

3. Get TelephonyManager

Obtain an instance of TelephonyManager using context.getSystemService(Context.TELEPHONY_SERVICE).

4. Retrieve Network Operator

Call telephonyManager.getNetworkOperator() to get the MCC+MNC string.

5. Parse MCC and MNC

Extract the first three digits for MCC and the rest for MNC from the networkOperator string.

6. Map to Country

Use a lookup table to map the obtained MCC to its corresponding country name.