Android Telephony SIM

1. Introduction

Android Telephony SIM is a set of APIs and functionalities provided by the Android operating system to interact with the SIM card inserted in an Android device. The SIM card is an essential component of a mobile phone, as it stores information related to the user's identity, contacts, and network credentials.

In this article, we will explore the Android Telephony SIM APIs, discuss their significance, and provide code examples to demonstrate their usage.

2. Android Telephony SIM APIs

2.1. TelephonyManager

The TelephonyManager class is the entry point for interacting with the telephony services on an Android device. It provides methods to access various telephony-related information, including SIM card details.

To access the TelephonyManager instance, you can use the following code snippet:

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

2.2. SIM Card Information

Once you have the TelephonyManager instance, you can retrieve information about the SIM card using the following methods:

  • getSimOperator() - Returns the MCC (Mobile Country Code) and MNC (Mobile Network Code) of the SIM operator.
  • getSimOperatorName() - Returns the name of the SIM operator.
  • getSimSerialNumber() - Returns the serial number of the SIM card.
  • getSubscriberId() - Returns the unique subscriber ID (IMSI) for the SIM card.
  • getSimState() - Returns the state of the SIM card (e.g., ready, absent, unknown).

Here's an example that demonstrates how to retrieve the SIM card information:

String simOperator = telephonyManager.getSimOperator();
String simOperatorName = telephonyManager.getSimOperatorName();
String simSerialNumber = telephonyManager.getSimSerialNumber();
String subscriberId = telephonyManager.getSubscriberId();
int simState = telephonyManager.getSimState();

2.3. SIM Card Events

Android provides a mechanism to receive notifications for SIM card related events, such as SIM card insertion, removal, and changes. To receive these events, you need to register a PhoneStateListener with the TelephonyManager.

Here's an example that demonstrates how to register a PhoneStateListener to receive SIM card events:

PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onSimStateChanged(int state) {
        // Handle SIM card state change
    }
};

telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_SIM_STATE);

2.4. SIM Card Permissions

To access the SIM card information and receive SIM card events, your Android application needs appropriate permissions in the manifest file. Add the following permissions to your manifest file:

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

3. Code Examples

3.1. Retrieving SIM Card Information

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

String simOperator = telephonyManager.getSimOperator();
String simOperatorName = telephonyManager.getSimOperatorName();
String simSerialNumber = telephonyManager.getSimSerialNumber();
String subscriberId = telephonyManager.getSubscriberId();
int simState = telephonyManager.getSimState();

3.2. Handling SIM Card Events

PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onSimStateChanged(int state) {
        switch (state) {
            case TelephonyManager.SIM_STATE_ABSENT:
                // SIM card removed
                break;
            case TelephonyManager.SIM_STATE_READY:
                // SIM card ready
                break;
            case TelephonyManager.SIM_STATE_UNKNOWN:
                // SIM card state unknown
                break;
        }
    }
};

telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_SIM_STATE);

4. Sequence Diagram

sequenceDiagram
    participant App
    participant TelephonyManager
    App -> TelephonyManager: getSystemService(Context.TELEPHONY_SERVICE)
    App -> TelephonyManager: getSimOperator()
    TelephonyManager -> Telecom Service: Request SIM Operator
    Telecom Service -> SIM Card: Retrieve SIM Operator
    SIM Card --> Telecom Service: SIM Operator
    Telecom Service --> TelephonyManager: SIM Operator
    App -> TelephonyManager: getSimOperatorName()
    TelephonyManager -> Telecom Service: Request SIM Operator Name
    Telecom Service -> SIM Card: Retrieve SIM Operator Name
    SIM Card --> Telecom Service: SIM Operator Name
    Telecom Service --> TelephonyManager: SIM Operator Name
    App -> TelephonyManager: getSimSerialNumber()
    TelephonyManager -> Telecom Service: Request SIM Serial Number
    Telecom Service -> SIM Card: Retrieve SIM Serial Number
    SIM Card --> Telecom Service: SIM Serial Number
    Telecom Service --> TelephonyManager: SIM Serial Number
    App -> TelephonyManager: getSubscriberId()
    TelephonyManager -> Telecom Service: Request