65

I need help checking whether a device has a sim card programatically. Please provide sample code.

Tim
  • 41,901
  • 18
  • 127
  • 145
Senthil Mg
  • 3,301
  • 4
  • 32
  • 49
  • What about CDMA phones that don't have sim cards? – Falmarri Oct 20 '10 at 18:52
  • @Senthil Mg Hey can you tell me how to get know if sim card is available in phone or not? I mean I tried with Telephony Manager but I can't get proper answer. Can you give me any simple example so that I can more understand. – anddev Jan 24 '12 at 05:32
  • @Mansi Vora,specify the problem you facing clearly,have you checked the below answer for this. – Senthil Mg Jan 24 '12 at 06:18
  • @Senthil Mg Yes I checked with below code and I have done my requirement. Thanks.. But now I want to know one more thing that when we dial a call from mobile at that time I want to check one condition so for that what should I do? At the time of Incoming call we used BroadcastReceiver at the same way how can we trace outgoing call.. Can you give me any idea.. I am confuse in this point. Thanks – anddev Jan 24 '12 at 06:35

4 Answers4

134

Use TelephonyManager.

http://developer.android.com/reference/android/telephony/TelephonyManager.html

As Falmarri notes, you will want to use getPhoneType FIRST of all, to see if you are even dealing with a GSM phone. If you are, then you can also get the SIM state.

TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    int simState = telMgr.getSimState();
            switch (simState) {
                case TelephonyManager.SIM_STATE_ABSENT:
                    // do something
                    break;
                case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
                    // do something
                    break;
                case TelephonyManager.SIM_STATE_PIN_REQUIRED:
                    // do something
                    break;
                case TelephonyManager.SIM_STATE_PUK_REQUIRED:
                    // do something
                    break;
                case TelephonyManager.SIM_STATE_READY:
                    // do something
                    break;
                case TelephonyManager.SIM_STATE_UNKNOWN:
                    // do something
                    break;
            }

EDIT:

Starting at API 26 (Android O Preview) you can query the SimState for individual sim slots by using getSimState(int slotIndex) ie:

int simStateMain = telMgr.getSimState(0);
int simStateSecond = telMgr.getSimState(1);

official documentation

If you're developing with and older api, you can use TelephonyManager's

String getDeviceId (int slotIndex)
//returns null if device ID is not available. ie. query slotIndex 1 in a single sim device

int devIdSecond = telMgr.getDeviceId(1);

//if(devIdSecond == null)
// no second sim slot available

which was added in API 23 - docs here

HenriqueMS
  • 3,864
  • 2
  • 30
  • 39
Charlie Collins
  • 8,806
  • 4
  • 32
  • 41
  • thanks for your answer,let me know how to check whether the phone number entered is valid from phone directory – Senthil Mg Oct 21 '10 at 09:23
  • int simStateSecond = telMgr.getSimState(1); it is giving error method getSimState in class TelephonyManager cannot be applied to given types; int simState = telMgr.getSimState(0); – MSD Jan 11 '19 at 13:09
17

You can check with the below code :

public static boolean isSimSupport(Context context)
    {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);  //gets the current TelephonyManager
        return !(tm.getSimState() == TelephonyManager.SIM_STATE_ABSENT);

    }
Rohit Sharma
  • 13,787
  • 8
  • 57
  • 72
Arun kumar
  • 1,894
  • 3
  • 22
  • 28
3

Found another way to do this.

public static boolean isSimStateReadyorNotReady() {
        int simSlotCount = sSlotCount;
        String simStates = SystemProperties.get("gsm.sim.state", "");
        if (simStates != null) {
            String[] slotState = simStates.split(",");
            int simSlot = 0;
            while (simSlot < simSlotCount && slotState.length > simSlot) {
                String simSlotState = slotState[simSlot];
                Log.d("MultiSimUtils", "isSimStateReadyorNotReady() : simSlot = " + simSlot + ", simState = " + simSlotState);
                if (simSlotState.equalsIgnoreCase("READY") || simSlotState.equalsIgnoreCase("NOT_READY")) {
                    return true;
                }
                simSlot++;
            }
        }
        return false;
    }
kakopappa
  • 5,023
  • 5
  • 54
  • 73
2

Thanks @Arun kumar answer, kotlin version as below

fun isSIMInserted(context: Context): Boolean {
    return TelephonyManager.SIM_STATE_ABSENT != (context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).simState
}
Graham Smith
  • 25,627
  • 10
  • 46
  • 69
Seven
  • 177
  • 1
  • 5