2010-10-20 135 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; 
    } 
相关问题