Android網絡連接的理解

Android的網絡連接類型大致分爲wifi和mobile兩種,wifi比較容易理解,難理解一點的是mobile類型。

首先,檢查當前是否有網絡連接採用的方法如下:

public static boolean isNetworkConnected(Context context) {  
            NetworkInfo networkInfo = ((ConnectivityManager) context  
                    .getApplicationContext().getSystemService("connectivity"))  
                    .getActiveNetworkInfo();  
            if (networkInfo != null) {  
                return networkInfo.isConnectedOrConnecting();  
            }  
            return false;  
}  

然後,區分爲wifi還是mobile網絡,採用的方法如下:

        private void checkNetworkType(Context context) {  
            NetworkInfo networkInfo = ((ConnectivityManager) context  
                    .getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();  
            if (networkInfo != null) {  
                if (!"wifi".equals(networkInfo.getTypeName().toLowerCase())) {  
                    checkApn(context); //後續進行解釋 
                    return;  
                }  
                this.mUseWap = false;  //顯然,如果是wifi網絡,自然不使用wap
            }  
        }  

如果不是wifi網絡就要判斷它是否爲wap還是net了,查看當前的apn設置,

        private void checkApn(Context context) {  
            ContentResolver contentResolver = context.getContentResolver();  
            Uri uri = Uri.parse("content://telephony/carriers/preferapn");  
            String[] apnInfo = new String[3];  
            apnInfo[0] = "apn";  
            apnInfo[1] = "proxy";  
            apnInfo[2] = "port";  
      
            Cursor cursor = contentResolver.query(uri, apnInfo, null, null, null);  
            if (cursor != null) {  
                while (cursor.moveToFirst()) {  
                    this.mApn = cursor.getString(cursor.getColumnIndex("apn"));  
                    this.mProxy = cursor.getString(cursor.getColumnIndex("proxy"));  
                    this.mPort = cursor.getString(cursor.getColumnIndex("port"));  
      
                    // 代理爲空   
                    if ((this.mProxy == null) || (this.mProxy.length() <= 0)) {  
                        String apn = this.mApn.toUpperCase();  
                          
                        // 中國移動WAP設置:APN:CMWAP;代理:10.0.0.172;端口:80   
                        // 中國聯通WAP設置:APN:UNIWAP;代理:10.0.0.172;端口:80   
                        // 中國聯通WAP設置(3G):APN:3GWAP;代理:10.0.0.172;端口:80   
                        if ((apn.equals("CMWAP")) || (apn.equals("UNIWAP")) || (apn.equals("3GWAP"))) {  
                            this.mUseWap = true;  
                            this.mProxy = "10.0.0.172";  
                            this.mPort = "80";  
                            break;  
                        }  
                          
                        // 中國電信WAP設置:APN(或者接入點名稱):CTWAP;代理:10.0.0.200;端口:80   
                        if (apn.equals("CTWAP")) {  
                            this.mUseWap = true;  
                            this.mProxy = "10.0.0.200";  
                            this.mPort = "80";  
                            break;  
                        }  
                          
                    }  
                    this.mPort = "80";  
                    this.mUseWap = true;  
                    break;  
                }  
      
            }  
      
            this.mUseWap = false;  
            cursor.close();  
        } 

而對於短彩來說,不管當前是否有數據連接,短彩的發送走的TYPE_MOBILE通路。如果沒有TYPE_MOBILE通路他就會啓動一次mobile通路.

mConnMgr.startUsingNetworkFeature(
                ConnectivityManager.TYPE_MOBILE, PhoneFactory.getFeature(Phone.FEATURE_ENABLE_MMS, mPhoneId))

等待發送完成後,又將該通路進行關閉。不過,短信是通過WAP網來發送的,所以需要設置代理。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章