简体   繁体   English

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

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

I need help checking whether a device has a sim card programatically.我需要帮助以编程方式检查设备是否具有 SIM 卡。 Please provide sample code.请提供示例代码。

Use TelephonyManager.使用电话管理器。

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

As Falmarri notes, you will want to use getPhoneType FIRST of all, to see if you are even dealing with a GSM phone.作为Falmarri笔记,你要使用所有的getPhoneType首先,要看看你甚至处理一个GSM电话。 If you are, then you can also get the SIM state.如果是,那么您还可以获得 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:编辑:

Starting at API 26 ( Android O Preview ) you can query the SimState for individual sim slots by using getSimState(int slotIndex) ie:从 API 26( Android O 预览版)开始,您可以使用getSimState(int slotIndex)查询 SimState 以获取单个 sim 插槽,即:

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

official documentation 官方文件

If you're developing with and older api, you can use TelephonyManager's如果您正在使用较旧的 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

which was added in API 23 - docs here这是在 API 23 中添加的 - 文档在这里

You can check with the below code :您可以使用以下代码进行检查:

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

    }

Found another way to do this.找到了另一种方法来做到这一点。

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

Thanks @Arun kumar answer, kotlin version as below感谢@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