雙卡的時候尤误,用TelephonyManager的getSimOperatorName拿到的是默認打電話的運營商名稱蜘澜,這api有點坑掏父。
注意:
getNetworkOperator 不是獲取上網卡運營商
getSimOperatorName 不是獲取上網卡運營商的名稱
官方文檔只支持5.1及其之后的系統(tǒng)提供雙卡API兜叨。對于之前的系統(tǒng)版本档玻,就無能為力了躏率。
1躯畴、5.0及其之前 android.os.Build.VERSION.SDK_INT<=21
這部分放棄民鼓,將其歸入沒有獲取到系統(tǒng)權限的一樣,當做讀不出運營商看待蓬抄。
2丰嘉、5.1及其之后 android.os.Build.VERSION.SDK_INT>=22
精準獲取上網卡運營商的調用如下:
public static int getCurrentCarrierType(Context context) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
return -1;
}
if (context == null) {
return -1;
}
TelephonyManager telMag = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (telMag == null) {
return -1;
}
return parseOperatorCode(telMag.getSimOperator());
}
public static int parseOperatorCode(String operatorCode) {
if (operatorCode == null || "".equals(operatorCode)) return -1;
switch (operatorCode) {
case "46000":
case "46002":
case "46007":
case "46008":
return MOBILE;
case "46001":
case "46006":
case "46009":
return UNICOM;
case "46003":
case "46005":
case "46011":
return TELECOM;
}
return -1;
}