我可以找到网络提供商的MCC + MNC用于多种模拟

时间:2015-12-22 05:41:27

标签: android telephonymanager cellinfo

例如,我有一个带有Airtel和Vodafone sims的双卡双待手机。我想知道我们可以获得两个网络提供商的MCC和MNC。

我知道

  

使用getPhoneCount()我们可以知道设备有多少个SIM卡   使用getNetworkOperator(),我们可以获得有效网络运营商的MCC + MNC。

1 个答案:

答案 0 :(得分:0)

以前不是那么容易,但是在 Lolipop 版本之后,您可以执行此操作。 这是我的代码:

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                SubscriptionManager subManager = (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
                if (subManager.getActiveSubscriptionInfoCount() >= 1) {
                    List<SubscriptionInfo> subscriptionInfoList;
                    subscriptionInfoList=subManager.getActiveSubscriptionInfoList();
                    String carrierName = subscriptionInfoList.get(0).getCarrierName().toString();
                    int mccSlot1 = subscriptionInfoList.get(0).getMcc();
                    int mncSlot1 = subscriptionInfoList.get(0).getMnc();
                    Toast.makeText(MainActivity.this, "Sim slot 1 " + mccSlot1 + " " + mncSlot1 + "Name"+carrierName, Toast.LENGTH_SHORT).show();
                    String carrierName2 = subscriptionInfoList.get(1).getCarrierName().toString();
                    int mccSlot2 = subscriptionInfoList.get(1).getMcc();
                    int mncSlot2 = subscriptionInfoList.get(1).getMnc();
                    Toast.makeText(MainActivity.this, "Sim slot 2 " + mccSlot2 + " " + mncSlot2+"Name"+carrierName2, Toast.LENGTH_SHORT).show();

                }
            } else {
                TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                if (telephonyManager == null) {
                    return;
                }
                String mCCMncCode = telephonyManager.getNetworkOperator();
                String mccCode = null;
                String mncCode = null;
                if (TextUtils.isEmpty(mCCMncCode)) {
                    return;
                }

                final int MNC_CODE_LENGTH = 3;
                if (mCCMncCode.length() >= MNC_CODE_LENGTH) {
                    mccCode = mCCMncCode.substring(0, MNC_CODE_LENGTH);
                }

                if (mCCMncCode.length() > MNC_CODE_LENGTH) {
                    mncCode = mCCMncCode.substring(MNC_CODE_LENGTH);
                }
                Toast.makeText(MainActivity.this, "Lower than API 22 " + mccCode + " " + mncCode, Toast.LENGTH_SHORT).show();
                return;
            }

在获得许可后,只需将此代码添加到on create方法中即可。

   <uses-permission android:name="android.permission.READ_PHONE_STATE" />

注意:如果您在marshmellow,nougat和oreo等最新的android版本上进行测试,则必须给checkselfpermission(运行时权限)。 对于Dual Sim检查,请查看:Answer