Frage

Ich brauche Hilfe zu prüfen, ob ein Gerät eine SIM-Karte programmatisch hat. Bitte geben Sie Beispielcode.

War es hilfreich?

Lösung

Mit TelephonyManager.

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

Wie Falmarri Notizen Sie wird wollen Gebrauch getPhoneType in erster Linie, um zu sehen, ob Sie auch mit einem GSM-Telefon handeln. Wenn Sie sind, dann können Sie auch den SIM-Zustand erhalten.

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:

Ab API 26 ( Android O Vorschau ) können Sie die SimState für einzelne SIM-Slots abfragen können mit getSimState(int slotIndex) dh:

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

offizielle Dokumentation

Wenn Sie mit und ältere api entwickelst, können Sie 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

, die 23 in API hinzugefügt wurde - docs hier

Andere Tipps

Sie können mit dem folgenden Code überprüfen:

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

    }

Gefunden einen anderen Weg, dies zu tun.

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;
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top