2013-08-21 95 views
0

我試圖檢查一個Android設備,以確保其中包含T-Mobile或AT & T SIM(它的確如此 - 我已經在3個設備上測試了這個SIM卡)ATT sim卡 - 結果是相同的),但是應用程序不斷顯示錯誤提示「請插入TMobile的或AT & T的sim卡」,並在此行結束: 檢查SIM卡的結果是否爲假

showAlert(getString(R.string.insert_sm_dialog)); 

而不是通過代碼移動,因爲它應該,因爲該設備包含AT & T Sim卡。

來源:

@Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); 
      int networkType = tm.getNetworkType(); 
      int phoneType = tm.getPhoneType(); 
      handler = new XmlParserHandlerFinal(); 
      int version = android.os.Build.VERSION.SDK_INT; 
      if (phoneType == TelephonyManager.PHONE_TYPE_CDMA 
        || (phoneType != TelephonyManager.PHONE_TYPE_GSM 
        && networkType != TelephonyManager.NETWORK_TYPE_GPRS 
        && networkType != TelephonyManager.NETWORK_TYPE_EDGE 
        && networkType != TelephonyManager.NETWORK_TYPE_HSDPA 
        && networkType != TelephonyManager.NETWORK_TYPE_HSPA 
        && networkType != TelephonyManager.NETWORK_TYPE_HSPAP 
        && networkType != TelephonyManager.NETWORK_TYPE_HSUPA 
        && networkType != TelephonyManager.NETWORK_TYPE_UMTS && networkType != TelephonyManager.NETWORK_TYPE_LTE)) { 
       // If the phone type is CDMA or 
       // the phone phone type is not GSM and the network type is none of 
       // the network types indicated in the statement 
       // Display incompatibility message 
       showAlert(getString(R.string.incomp_sm_dialog)); 
       // Network type is looked because some tablets have no phone type. 
       // We rely on network type in such cases 
      } else if (!(tm.getSimState() == TelephonyManager.SIM_STATE_ABSENT 
        || (tm.getSimOperator()) 
        .equals(getString(R.string.numeric_tmo)) || (tm 
          .getSimOperator()).equals(getString(R.string.numeric_att)))) { 
       // if SIM is present and is NOT a T-Mo network SIM, 
       // display Error message alert indicating to use SM SIM 
       showAlert(getString(R.string.insert_sm_dialog)); 
      }// No SIM or SIM with T-Mo MNC MCC present 
      else if (version < VERSION_CODES.ICE_CREAM_SANDWICH) { 
       // Initial UI setup for versions lower than ICS 
       setContentView(R.layout.update); 
       mUpdateButton = (Button) findViewById(R.id.update_button); 

       mUpdateButton.setOnClickListener(this); 

      } else {// ICS and up 


       if ((tm.getSimOperator()).equals(getString(R.string.numeric_tmo)) 
         || (tm.getSimOperator()) 
         .equals(getString(R.string.numeric_att))) { 
        task = new NetworkTask(); 
        task.execute(""); 
        // Device has T-Mo network SIM card MCC and MNC correctly 
        // populated 
        // Reduce number of steps to 6 
        TotalSteps = 6; 
       } 

      } 
     } 
+0

什麼值爲'tm.getSimOperator()'返回? 「R.string.numeric_tmo」和「R.string.numeric_att」的值是什麼? – Michelle

+0

tm.getSimOperator()返回310410 - R.string.numeric_tmo的值爲310260,R.string.numeric_att的值爲310410 – NoobNinja

+0

嘗試將條件分解爲多個部分,以查看哪個部分明確跳過它。 – Michelle

回答

1
} else if (!(tm.getSimState() == TelephonyManager.SIM_STATE_ABSENT 
     || (tm.getSimOperator()) 
     .equals(getString(R.string.numeric_tmo)) || (tm 
       .getSimOperator()).equals(getString(R.string.numeric_att)))) { 

     // if SIM is present and is NOT a T-Mo network SIM, 
     // display Error message alert indicating to use SM SIM 
     showAlert(getString(R.string.insert_sm_dialog)); 
}// No SIM or SIM with T-Mo MNC MCC present 

如果我沒有記錯的話,這個代碼意味着如果你有一個SIM卡插入時,它會顯示該消息。你正在檢查SIM_STATE_ABSENT,但否定它。缺席

  • 如果getSimState()回報,那麼整個事情是true,它被否定,false

  • 如果getSimState()返回而不是不存在,則它檢查TMO/ATT。如果這兩個都是真的,那麼它們全部否定爲false

您應該能夠通過否定移動運營商檢查,以解決這個問題:

} else if(tm.getSimState() == TelephonyManager.SIM_STATE_ABSENT 
     || !(tm.getSimOperator().equals(getString(R.string.numeric_tmo)) 
       || (tm.getSimOperator().equals(getString(R.string.numeric_att)))) { 
    // show error 
} 
+0

非常好 - 謝謝! – NoobNinja