61

我需要帮助以编程方式检查设备是否有 sim 卡。请提供示例代码。

4

4 回答 4

128

使用电话管理器。

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

正如 Falmarri 所指出的,您首先需要使用getPhoneType,以查看您是否正在处理 GSM 电话。如果是,那么您还可以获得 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;
            }

编辑:

从 API 26 ( Android O Preview ) 开始,您可以使用以下命令查询 SimState 中的单个 sim 插槽getSimState(int slotIndex)

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

官方文档

如果您正在使用更旧的 api 进行开发,则可以使用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

这是在 API 23 中添加的 -此处的文档

于 2010-10-20T22:13:19.907 回答
15

您可以使用以下代码进行检查:

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

    }
于 2015-08-10T13:21:20.640 回答
3

找到了另一种方法来做到这一点。

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;
    }
于 2017-05-22T11:30:08.450 回答
1

感谢@Arun kumar 的回答,kotlin 版本如下

fun isSIMInserted(context: Context): Boolean {
    return TelephonyManager.SIM_STATE_ABSENT != (context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).simState
}
于 2021-02-01T04:02:21.643 回答