Android編程擷取網路連接方式及判斷手機卡所屬電訊廠商的方法_Android

來源:互聯網
上載者:User

本文執行個體講述了Android編程擷取網路連接方式及判斷手機卡所屬電訊廠商的方法。分享給大家供大家參考,具體如下:

問題:項目中寫的網路模組,感覺有點亂:兩套代碼 --模擬器、真機,維護起來十分麻煩。

解決辦法:代碼自動去檢查到那種網路環境,然後調用不同的連網方式。

查看了模擬器上預設的存取點:移動網路 -- APN = "internet"

1、通過擷取apn的名稱,來判斷網路

// 擷取Mobile網路下的cmwap、cmnetprivate 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;}

2、直接擷取代理參數: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;}/** * 擷取手機卡類型,移動、聯通、電信 *  */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"))    {      // 中國電信    }  }}

希望本文所述對大家Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.