[英]How can I check whether Sim Cards are installed in both sim slots in an android device?
[英]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.