Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

判斷網路連線方式與 SIM 卡電信商的方法

編輯:關於 Android 程式設計

問題背景

專案裡的網路模組寫得有點亂:模擬器一套、真機一套,維護起來很痛苦。

解法

讓程式自動偵測當前處於哪種網路環境,再決定要走哪種連線方式。查了一下模擬器預設的接入點,是「行動網路」且 APN 為 internet。

方法一:從 APN 名稱判斷

 // 獲取Mobile網絡下的cmwap、cmnet private int getCurrentApnInUse() { int type = NONET; Cursor cursor = context.getContentResolver().query(PREFERRED_APN_URI, new String[] { "_id", "apn", "type" }, null, null, null); cursor.moveToFirst(); int counts = cursor.getCount(); if(counts != 0){//適配平板外掛3G模塊情況 if (!cursor.isAfterLast()) { String apn = cursor.getString(1); //#777、ctnet 都是中國電信定制機接入點名稱,中國電信的接入點:Net、Wap都采用Net即非代理方式聯網即可 //internet 是模擬器上模擬接入點名稱 if (apn.equalsIgnoreCase("cmnet") || apn.equalsIgnoreCase("3gnet") || apn.equalsIgnoreCase("uninet") || apn.equalsIgnoreCase("#777") || apn.equalsIgnoreCase("ctnet") || apn.equalsIgnoreCase("internet")) { type = WIFIAndCMNET; } else if (apn.equalsIgnoreCase("cmwap") || apn.equalsIgnoreCase("3gwap") || apn.equalsIgnoreCase("uniwap")) { type = CMWAP; } }else{ //適配中國電信定制機,如海信EG968,上面方式獲取的cursor為空,所以換種方式 Cursor c = context.getContentResolver().query(PREFERRED_APN_URI,null, null, null, null); c.moveToFirst(); String user=c.getString(c.getColumnIndex("user")); if(user.equalsIgnoreCase("ctnet")){ type = WIFIAndCMNET; } c.close(); } }else{ type = WIFIAndCMNET;//平板外掛3G,采用非代理方式上網 } cursor.close(); return type; } 

這段程式背後的邏輯是:cmwap、3gwap、uniwap 這類需要透過代理伺服器上網,而 cmnet、ctnet、uninet 則是直連。兩者的連線寫法不同,所以必須先分辨清楚。程式中還額外處理了兩種特殊情況:某些電信客製機(如海信 EG968)用上述方式取到的 cursor 是空的,得改從 user 欄位判斷;平板外接 3G 模組時 cursor 也可能為空,此時預設當作直連處理。

方法二:直接看 proxy 欄位

其實不必硬記各家 APN 名稱,直接查 proxy 欄位有沒有值更乾脆:

 /** * MOBILE方式下獲取當前的網絡連接方式,代理或非代理 * */ public static String getCurrentApnInUse(Context context) { Cursor cursor = context.getContentResolver().query(PREFERRED_APN_URI, new String[] { "_id", "apn", "type", "proxy" }, null, null, null); cursor.moveToFirst(); if (cursor.isAfterLast()) { String apn = cursor.getString(3); if (apn == null) { apn = ""; } } return apn; }

這種做法的好处是不必隨電信商新增 APN 名稱而維護清單。不過要留意原範例有個邏輯瑕疵:判斷條件寫成 if (cursor.isAfterLast()),但 cursor 指到最後一筆之後時,getString() 反而會出錯;正確應為 if (!cursor.isAfterLast()),同時別忘了關閉 cursor。

判斷 SIM 卡電信商

 /** * 獲取手機卡類型,移動、聯通、電信 * */ private static int getMobileType(Context context) { int type = 0; TelephonyManager iPhoneManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); String iNumeric = iPhoneManager.getSimOperator(); if (iNumeric.length() > 0) { if (iNumeric.equals("46000") || iNumeric.equals("46002")) { // 中國移動 } else if (iNumeric.equals("46001")) { // 中國聯通 } else if (iNumeric.equals("46003")) { // 中國電信 } } }

getSimOperator() 回傳的是 MCC+MNC 組成的五碼數字,前三位是國家碼(460 為中國),後兩位才是電信商。上面只列了少數幾組,實際上各家電信還有多個號段(例如聯通的 46001、46006、46009,電信的 46003、46005、46011),正式專案建議改用 TelephonyManager.getSimOperatorName() 直接取得電信商名稱,省下維護清單的麻煩。

別忘了權限與執行期檢查

讀取 APN 需要 android.permission.READ_APN_STATE,查 SIM 資訊需要 android.permission.READ_PHONE_STATE。後者屬於危險權限,在 Android 6.0 以上必須執行期動態請求,否则 getSimOperator() 會拿到空字串。

另外要提醒的是,APN 資料庫屬於系統級資訊,各家廠牌與 Android 版本的實作差異不小,加上近年 SubscriptionManager(雙卡)與 ConnectivityManager.NetworkCapabilities 的問世,上述以 APN 為核心的判斷方式已逐漸被取代。新專案建議優先用 NetworkCapabilities.TRANSPORT_CELLULAR 判斷連線類型,只有需要區分代理/直連這類細節時,才需要回頭查 APN。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved