如何在Android设备中检查SIM卡是否可用?
-
09-10-2019 - |
题
我需要帮助检查设备是否在编程中具有SIM卡。请提供示例代码。
解决方案
使用TelephonyManager。
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)
IE:
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;
}
不隶属于 StackOverflow