java - 检查 SIM 卡结果出现误报

标签 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;
                }

            }
        }

最佳答案

} 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()返回not不存在,则它检查TMO/ATT。如果其中任何一个为真,则所有结果都被否定为

您应该能够通过将否定移至运算符检查来解决此问题:

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

关于java - 检查 SIM 卡结果出现误报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18362222/

相关文章:

java - Java 9 编译器中的 --release 标志是什么?

java - 我的服务器应用程序无法在 Eclipse 上运行

Java:(线程/计时器)每天早上 6 点运行一个函数

Android,如何以编程方式向按钮添加图标*和*文本?

android - 如何在不使用 php 的情况下使用 Rest webservice 从 android 访问外部数据库

java - 可点击的jbutton,如果是java的话

C++ 设计模式摆脱 if-then-else

java - 使用 Java 解析 XML 时出错

android - 如何在用户触摸事件时在 xml 布局上 move 图像?

php - 从第 2 项开始每 12 项添加 php 代码