¿Cómo puedo comprobar si la tarjeta SIM está disponible en un dispositivo Android?
-
09-10-2019 - |
Pregunta
necesito ayuda comprobación de si un dispositivo tiene una tarjeta SIM mediante programación. Sírvanse proporcionar código de ejemplo.
Solución
Uso TelephonyManager.
http://developer.android.com/reference/android/telephony/ TelephonyManager.html
Como notas Falmarri, que que desee utilizar getPhoneType En primer lugar, para ver si está incluso tratando con un teléfono GSM. Si es así, entonces también se puede obtener el estado de 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 la API 26 ( Android O Vista previa ) 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);
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 aquí
Otros consejos
Se puede comprobar con el siguiente código:
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);
}
Encontrados otra manera de hacer esto.
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;
}