Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 481
Senthil Mg Avatar asked Oct 20 '10 18:10

Senthil Mg


People also ask

How do I know if my Android phone has a SIM card?

On Android phones, you can usually find the SIM card slot in one of two places: under (or around) the battery or in a dedicated tray along the side of the phone.

How can you tell if there is a SIM card in your phone?

Depending on the type of phone, it could be placed behind the battery. In that case, you will have to open the back panel. For other phones, the SIM cards can be found on the side of the phone.

How do I access SIM card settings on Android?

Open the Settings app on your Samsung Galaxy smartphone and tap on Connections. Next, access the SIM card manager. This opens the SIM card manager screen, the go-to place to change the Dual SIM settings on your Samsung Galaxy smartphone with Android.


1 Answers

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

like image 193
Charlie Collins Avatar answered Oct 20 '22 06:10

Charlie Collins