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网来发送的,所以需要设置代理。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章