检查SIM卡结果是否为误

时间:2013-08-21 15:55:53

标签: java android if-statement telephonymanager

我正在尝试检查Android设备以确保它包含T-Mobile或AT& T SIM(它确实如此 - 我已经在3台设备上测试了这个和AT& T sim卡 - 结果是一样的)然而,该应用程序不断显示一个错误,指出“请插入一个TMobile或AT& T 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;
                }

            }
        }

1 个答案:

答案 0 :(得分: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
}