Comment puis-je vérifier si la carte SIM est disponible dans un appareil Android?
-
09-10-2019 - |
Question
Je besoin d'une vérification d'aide si un appareil a une carte sim programatically. S'il vous plaît fournir un exemple de code.
La solution
Utilisez TelephonyManager.
http://developer.android.com/reference/android/telephony/ TelephonyManager.html
notes Falmarri, vous veulent utiliser getPhoneType Tout d'abord, pour voir si vous traitez même avec un téléphone GSM. Si vous êtes, vous pouvez également obtenir l'état 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:
À partir de 26 API ( Android O Preview ), vous pouvez interroger le SimState pour les slots sim individuels en utilisant getSimState(int slotIndex)
-à-dire:
int simStateMain = telMgr.getSimState(0);
int simStateSecond = telMgr.getSimState(1);
Si vous développez avec et plus api, vous pouvez utiliser 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
qui a été ajouté dans l'API 23 - docs ici
Autres conseils
Vous pouvez vérifier avec le code ci-dessous:
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);
}
a trouvé une autre façon de le faire.
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;
}