我需要帮助检查设备是否以编程方式使用SIM卡。请提供示例代码。
答案 0 :(得分:108)
使用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 Preview )开始,您可以使用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中添加 - 文档here
答案 1 :(得分:7)
您可以查看以下代码:
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, null, null);
答案 2 :(得分:2)
找到了另一种方法。
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;
}
答案 3 :(得分:1)
感谢@Arun kumar 的回答,kotlin 版本如下
fun isSIMInserted(context: Context): Boolean {
return TelephonyManager.SIM_STATE_ABSENT != (context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).simState
}