How can I check whether the Sim Card is available in an android device?

AndroidTelephonymanager

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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);

	}

Solution 3 - Android

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;
    }

Solution 4 - Android

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
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSenthil MgView Question on Stackoverflow
Solution 1 - AndroidCharlie CollinsView Answer on Stackoverflow
Solution 2 - AndroidArun kumarView Answer on Stackoverflow
Solution 3 - AndroidkakopappaView Answer on Stackoverflow
Solution 4 - AndroidSevenView Answer on Stackoverflow