2010-10-20 40 views
38

Necesito ayuda para verificar si un dispositivo tiene una tarjeta SIM programaticamente. Proporcione un código de muestra.¿Cómo puedo verificar si la tarjeta SIM está disponible en un dispositivo Android?

+0

¿Qué pasa con los teléfonos CDMA que no tienen tarjetas SIM? – Falmarri

+0

@Senthil Mg Hola, ¿puedes decirme cómo saber si la tarjeta SIM está disponible en el teléfono o no? Quiero decir, lo intenté con el Administrador de telefonía, pero no puedo obtener la respuesta adecuada. ¿Puede darme un ejemplo simple para que yo pueda entender mejor? – anddev

+0

@Mansi Vora, especifique claramente el problema al que se enfrenta, ¿ha revisado la respuesta a continuación para esto? –

Respuesta

100

Use TelephonyManager.

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

Como notas Falmarri, que se desee utilizar getPhoneType En primer lugar, para ver si está incluso tratando con un teléfono GSM. Si es así, entonces también puede obtener el estado SIM.

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:

A partir de API 26 (Android O Vista previa de) puede consultar la SIMSTATE de ranuras de tarjeta SIM individuales utilizando getSimState(int slotIndex) es decir:

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

official documentation

Si está desarrollando con API y más, puede utilizar 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 

que se añadió en la API 23 - docs here

+0

gracias por su respuesta, hágamelo saber cómo comprobar si el número de teléfono ingresado es válido desde el directorio telefónico –

+20

¿Cómo funciona esto para dispositivos con doble SIM? – gonzobrains

8

Usted puede consultar con el código de abajo:

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

    } 
0

Encontré otra forma de hacerlo.

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