日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

android mcc mnc,SystemUI如何获得SIM卡相关的mcc/mnc值

發(fā)布時(shí)間:2023/12/20 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android mcc mnc,SystemUI如何获得SIM卡相关的mcc/mnc值 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

1. 存儲(chǔ)在SIM卡中的mcc/mnc

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

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.

*

* 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.

*

*

* @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í)候的計(jì)數(shù)來統(tǒng)計(jì)的,但是如果相關(guān)類中有SubscriptionInfo對(duì)象的話,是可以直接取到的:

int subId = mSubscriptionInfo.getSubscriptionId();

另一種phoneId則比較簡單了,它與sim卡數(shù)量有關(guān),單卡時(shí)為0,雙卡時(shí)根據(jù)sim slot位置分別取0和1。

1.2 通過SubscriptionInfo獲得

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

// SubscriptionInfo.java

/**

* @return the MCC.

*/

public int getMcc() {

return this.mMcc;

}

/**

* @return the MNC.

*/

public int getMnc() {

return this.mMnc;

}

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

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

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

2.1 通過ServiceState獲得

熟悉Android Telephony流程的朋友應(yīng)該都知道,CS、PS域的注冊(cè)狀態(tài),漫游狀態(tài),運(yùn)營商名字的顯示,網(wǎng)絡(luò)模式等都是用模板類ServiceState.java來保存的。

SystemUI中有不少類都注冊(cè)了PhoneStateListener這個(gè)callback,用來時(shí)刻關(guān)注設(shè)備的一些telephony相關(guān)狀態(tài),當(dāng)網(wǎng)絡(luò)服務(wù)狀態(tài)有變化時(shí),會(huì)回調(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ù)對(duì)應(yīng)的OperatorNumeric是一樣的,所以getOperatorNumeric()默認(rèn)取了voice的。

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

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

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

在MTK源碼中,MtkServiceStateTracker在處理PLMN String即mcc/mnc時(shí),會(huì)通過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;

}

由此可知,只要在需要取的類中,注冊(cè)一個(gè)監(jiān)聽“ACTION_LOCATED_PLMN_CHANGED”的BroadcastReceiver就行了,在設(shè)備開機(jī)之后便可以第一時(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值,接下來用它操作即可

...

}

}

};

總結(jié)

以上是生活随笔為你收集整理的android mcc mnc,SystemUI如何获得SIM卡相关的mcc/mnc值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。