Question

I'm attempting to check an Android device to ensure it contains a T-Mobile or AT&T SIM (which it does - I've tested this on 3 devices w AT&T sim cards - results are the same) however the app continually displays an error stating "Please Insert a TMobile or AT&T SIM" and ends up on this line:

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

instead of moving through the code as it should since the devices contain AT&T Sim Cards.

SOURCE:

  @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;
                }

            }
        }
Was it helpful?

Solution

} 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

If I'm not mistaken, this code means that if you have a SIM card inserted, it will show the message. You're checking for SIM_STATE_ABSENT, but negating it.

  • If getSimState() returns absent, then the whole thing is true, which is negated to false.

  • If getSimState() returns not absent, then it checks for TMO/ATT. If either of these are true, again, it's all negated to false.

You should be able to fix this by moving the negation to the operator check:

} 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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top