SystemUI如何獲得SIM卡相關(guān)的mcc/mnc值

SystemUI獲得SIM卡相關(guān)的mcc/mnc值,分兩種情況討論

1. 存儲在SIM卡中的mcc/mnc

這個值是存儲在SIM卡IMSI(國際移動用戶識別碼 International Mobile Subscriber Identification Number)中的固定值,不會被更改。有以下兩種途徑可以取得。

1.1 通過TelephonyManager獲得

在TelephonyManager中有如下方法:

//TelephonyManager.java

    /**
     * Returns the MCC+MNC (mobile country code + mobile network code) of the
     * provider of the SIM for a particular subscription. 5 or 6 decimal digits.
     * <p>
     * Availability: SIM state must be {@link #SIM_STATE_READY}
     *
     * @see #getSimState
     *
     * @param subId for which SimOperator is returned
     * @hide
     */
    public String getSimOperatorNumeric(int subId) {
        int phoneId = SubscriptionManager.getPhoneId(subId);
        return getSimOperatorNumericForPhone(phoneId);
    }

    /**
     * Returns the MCC+MNC (mobile country code + mobile network code) of the
     * provider of the SIM for a particular subscription. 5 or 6 decimal digits.
     * <p>
     *
     * @param phoneId for which SimOperator is returned
     * @hide
     */
    public String getSimOperatorNumericForPhone(int phoneId) {
        return getTelephonyProperty(phoneId,
                TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC, "");
    }

↓↓↓

  • 由于subId并不固定,是根據(jù)放進(jìn)sim卡槽時候的計數(shù)來統(tǒng)計的,但是如果相關(guān)類中有SubscriptionInfo對象的話,是可以直接取到的:
     int subId = mSubscriptionInfo.getSubscriptionId();
    
  • 另一種phoneId則比較簡單了,它與sim卡數(shù)量有關(guān),單卡時為0,雙卡時根據(jù)sim slot位置分別取0和1。

1.2 通過SubscriptionInfo獲得

在有些特殊情況下,比如SIM卡處于PIN碼LOCK狀態(tài)時,1.1所提到的方法是取不到的,這個時候只能通過SubscriptionInfo來取。

// SubscriptionInfo.java

    /**
     * @return the MCC.
     */
    public int getMcc() {
        return this.mMcc;
    }

    /**
     * @return the MNC.
     */
    public int getMnc() {
        return this.mMnc;
    }

注意,由于這個方法取到的mcc/mnc均為int值,比如中國聯(lián)通的“46001”,則有mcc為“460”,mnc為“1”,與固定String字符串進(jìn)行匹配比對的話,需要先將String拆分為兩部分后分別強轉(zhuǎn)成int型后才可進(jìn)行比對。

2. SIM卡注冊網(wǎng)絡(luò)的mcc/mnc

非漫游情況下,注冊網(wǎng)絡(luò)的mcc/mnc就是SIM卡中存儲的。但是如果你的SIM卡在其他國家并沒有該運營商的基站,只能通過漫游到其他運營商的網(wǎng)絡(luò)上維持服務(wù)時,注冊網(wǎng)絡(luò)的mcc/mnc對應(yīng)的就是該運營商的值,與SIM卡無關(guān)了。

2.1 通過ServiceState獲得

熟悉Android Telephony流程的朋友應(yīng)該都知道,CS、PS域的注冊狀態(tài),漫游狀態(tài),運營商名字的顯示,網(wǎng)絡(luò)模式等都是用模板類ServiceState.java來保存的。
SystemUI中有不少類都注冊了PhoneStateListener這個callback,用來時刻關(guān)注設(shè)備的一些telephony相關(guān)狀態(tài),當(dāng)網(wǎng)絡(luò)服務(wù)狀態(tài)有變化時,會回調(diào)其onServiceStateChanged(ServiceState serviceState)方法,這樣我們就可以直接從ServiceState里面取了。

// ServiceState.java

    /**
     * Get current registered operator numeric id.
     *
     * In GSM/UMTS, numeric format is 3 digit country code plus 2 or 3 digit
     * network code.
     *
     * @return numeric format of operator, null if unregistered or unknown
     */
    /*
     * The country code can be decoded using
     * {@link com.android.internal.telephony.MccTable#countryCodeForMcc(int)}.
     */
    public String getOperatorNumeric() {
        return mVoiceOperatorNumeric;
    }

    /**
     * Get current registered voice network operator numeric id.
     * @return numeric format of operator, null if unregistered or unknown
     * @hide
     */
    public String getVoiceOperatorNumeric() {
        return mVoiceOperatorNumeric;
    }

    /**
     * Get current registered data network operator numeric id.
     * @return numeric format of operator, null if unregistered or unknown
     * @hide
     */
    public String getDataOperatorNumeric() {
        return mDataOperatorNumeric;
    }

一般來說,voice語音業(yè)務(wù)和data數(shù)據(jù)業(yè)務(wù)對應(yīng)的OperatorNumeric是一樣的,所以getOperatorNumeric()默認(rèn)取了voice的。

2.2 通過監(jiān)聽Telephony廣播獲得

由于該Intent action為MTK新增的,故以下方法介紹均以MTK源碼為基礎(chǔ)。

上面的方法必須在voice與data均注冊成功的前提下才能獲得,但是在一些很特殊的環(huán)境下,比如SIM卡雖然漫游上了某個其他運營商的網(wǎng)絡(luò),但由于兩家運營商之間并沒有協(xié)議,導(dǎo)致無法注冊上服務(wù),此時voice和data取得的OperatorNumeric均為空的。

在MTK源碼中,MtkServiceStateTracker在處理PLMN String即mcc/mnc時,會通過action為“TelephonyIntents.ACTION_LOCATED_PLMN_CHANGED”的廣播,把它作為extra參數(shù)傳遞出去。

// MtkServiceStateTracker.java

    private void updateLocatedPlmn(String plmn) {

        if (((mLocatedPlmn == null) && (plmn != null)) ||
            ((mLocatedPlmn != null) && (plmn == null)) ||
            ((mLocatedPlmn != null) && (plmn != null) && !(mLocatedPlmn.equals(plmn)))) {
            log("updateLocatedPlmn(),previous plmn= " + mLocatedPlmn + " ,update to: " + plmn);
            Intent intent = new Intent(TelephonyIntents.ACTION_LOCATED_PLMN_CHANGED);
            if (TelephonyManager.getDefault().getPhoneCount() == 1) {
                intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
            }
            intent.putExtra(TelephonyIntents.EXTRA_PLMN, plmn);

            ...
            mPhone.getContext().sendStickyBroadcastAsUser(intent, UserHandle.ALL);
        }

        mLocatedPlmn = plmn;
    }

由此可知,只要在需要取的類中,注冊一個監(jiān)聽“ACTION_LOCATED_PLMN_CHANGED”的BroadcastReceiver就行了,在設(shè)備開機之后便可以第一時間拿到漫游網(wǎng)絡(luò)的mcc/mnc值,具體如下:

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (action.equals(TelephonyIntents.ACTION_LOCATED_PLMN_CHANGED)) {
                mLocatedPlmn = intent.getStringExtra(TelephonyIntents.EXTRA_PLMN);
                // mLocatedPlmn即為漫游網(wǎng)絡(luò)的mcc/mnc值,接下來用它操作即可
                ...
            }
        }
    };
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容