2010-10-20 98 views
38

我需要幫助檢查設備是否有編程的SIM卡。請提供示例代碼。如何檢查SIM卡是否可用於Android設備?

+0

那些沒有SIM卡的CDMA手機呢? – Falmarri 2010-10-20 18:52:18

+0

@Senthil Mg你能告訴我如何知道sim卡是否可以在手機中使用?我的意思是我嘗試過使用電話管理器,但我無法得到正確的答案。你能否給我一個簡單的例子,以便我能更好地理解。 – anddev 2012-01-24 05:32:17

+0

@Mansi Vora,明確你面對的問題,你是否檢查了下面的答案。 – 2012-01-24 06:18:11

回答

100

使用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Ø預覽),您可以通過使用getSimState(int slotIndex)查詢SIMSTATE個人卡插槽,即:

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

official documentation

如果你有和年長的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

+0

感謝您的回答,請讓我知道如何檢查從手機目錄輸入的電話號碼是否有效 – 2010-10-21 09:23:11

+20

這對於雙SIM設備如何工作? – gonzobrains 2013-05-02 22:52:16

8

你可以用下面的代碼檢查:

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

    } 
0

找到了另一種方法來做到這一點。

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; 
    } 
相關問題