繁体   English   中英

如何检查 SIM 卡是否可用于 Android 设备?

[英]How can I check whether the Sim Card is available in an android device?

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

使用电话管理器。

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 预览版)开始,您可以使用getSimState(int slotIndex)查询 SimState 以获取单个 sim 插槽,即:

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 中添加的 - 文档在这里

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

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

    }

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

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

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

fun isSIMInserted(context: Context): Boolean {
    return TelephonyManager.SIM_STATE_ABSENT != (context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).simState
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM