Domanda

Ho bisogno di aiuto verificare se un dispositivo è dotato di una scheda SIM programatically. Si prega di fornire il codice di esempio.

È stato utile?

Soluzione

Usa TelephonyManager.

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

Come note Falmarri, è voglio usare getPhoneType Prima di tutto, per vedere se si sta ancora trattando con un telefono cellulare GSM. Se si, allora si può anche ottenere lo stato di 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;
            }

Modifica

A partire da API 26 ( Android O Anteprima ) è possibile interrogare il SimState per i singoli slot sim utilizzando getSimState(int slotIndex) vale a dire:

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

documentazione ufficiale

Se si sta sviluppando con e api più anziani, è possibile utilizzare 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

che è stata aggiunta in API 23 - docs qui

Altri suggerimenti

È possibile controllare con il codice qui sotto:

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

    }

Trovato un altro modo per farlo.

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;
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top