andriod 获取当前插入SIM的卡槽id及MNCMCC

sancaiodm Android应用 2021-08-24 1541 0

 

  static boolean supportMSim() {

        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1;

    }



static SmsManager getSmsManager(final int subId) {

        if (supportMSim()) {

            return SmsManager.getSmsManagerForSubscriptionId(subId);

        } else {

            return SmsManager.getDefault();

        }

    }


    public static List<SubscriptionInfo> getSelectableSubscriptionInfoList(Context context) {

        SubscriptionManager subManager = context.getSystemService(SubscriptionManager.class);

        List<SubscriptionInfo> availableList = subManager.getAvailableSubscriptionInfoList();

        if (availableList == null) {

            return null;

        } else {

            // Multiple subscriptions in a group should only have one representative.

            // It should be the current active primary subscription if any, or any

            // primary subscription.

            List<SubscriptionInfo> selectableList = new ArrayList<>();

            Map<ParcelUuid, SubscriptionInfo> groupMap = new HashMap<>();


            for (SubscriptionInfo info : availableList) {

                // Opportunistic subscriptions are considered invisible

                // to users so they should never be returned.

                if (!isSubscriptionVisible(subManager, context, info)) continue;


                ParcelUuid groupUuid = info.getGroupUuid();

                if (groupUuid == null) {

                    // Doesn't belong to any group. Add in the list.

                    selectableList.add(info);

                } else if (!groupMap.containsKey(groupUuid)

                        || (groupMap.get(groupUuid).getSimSlotIndex() == INVALID_SIM_SLOT_INDEX

                        && info.getSimSlotIndex() != INVALID_SIM_SLOT_INDEX)) {

                    // If it belongs to a group that has never been recorded or it's the current

                    // active subscription, add it in the list.

                    selectableList.remove(groupMap.get(groupUuid));

                    selectableList.add(info);

                    groupMap.put(groupUuid, info);

                }


            }

            return selectableList;

        }

    }



       final Context context = this.getContext();

      SubscriptionInfo  mSubscriptionInfo = context.getSystemService(SubscriptionManager.class) .getActiveSubscriptionInfoForSimSlotIndex(slotId);

        TelephonyManager tm = context.getSystemService(TelephonyManager.class);

        if (mSubscriptionInfo != null) {

            mTelephonyManager = context.getSystemService(TelephonyManager.class) .createForSubscriptionId(mSubscriptionInfo.getSubscriptionId());

        } else if(isValidSlotIndex(slotId, tm)) {

            mTelephonyManager = tm;

        } else {

            mTelephonyManager = null;

        }


private boolean isValidSlotIndex(int slotIndex, TelephonyManager telephonyManager) {

        return slotIndex >= 0 && slotIndex < telephonyManager.getPhoneCount();

    }


    static int[] getMccMnc(final Context context, final int subId) {

        final int[] mccMnc = new int[] { 0, 0 };

        if (Utils.supportMSim()) {

            final SubscriptionManager subscriptionManager = SubscriptionManager.from(context);

            final SubscriptionInfo subInfo = subscriptionManager.getActiveSubscriptionInfo(subId);

            if (subInfo != null) {

                mccMnc[0] = subInfo.getMcc();

                mccMnc[1] = subInfo.getMnc();

            }

        } else {

            final TelephonyManager telephonyManager =

                    (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

            final String mccMncString = telephonyManager.getSimOperator();

            try {

                mccMnc[0] = Integer.parseInt(mccMncString.substring(0, 3));

                mccMnc[1] = Integer.parseInt(mccMncString.substring(3));

            } catch (Exception e) {

                Log.w(MmsService.TAG, "Invalid mcc/mnc from system " + mccMncString + ": " + e);

                mccMnc[0] = 0;

                mccMnc[1] = 0;

            }

        }

        return mccMnc;

    }


评论