如何检查SIM卡是否可用于Android设备?
我需要帮助检查设备是否有编程的SIM卡。 请提供示例代码。
使用TelephonyManager。
http://developer.android.com/reference/android/telephony/TelephonyManager.html
正如Falmarri指出的那样,您将首先使用getPhoneType FIRST,以查看您是否正在处理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 )开始,您可以通过使用getSimState(int slotIndex)
来查询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 – 文档在这里
你可以检查下面的代码:
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); }
find另一种方法来做到这一点。
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; }