移动联通电信wap和net接入点判断

在android网络开发中,对接入点判断是必不可少的,因为连接网络的时候如果是wap接入点,需要设置代。移动联通wap(代理相同:10.0.0.172:80),电信wap(代理:10.0.0.200:80)
接入点类型:
Net网络:运营商(移动联通电信)net网络,wifi,usb网络共享 
Wap网络:移动联通wap(代理相同:10.0.0.172:80),电信wap(代理:10.0.0.200:80)
这样看来就可以抽象出三种网络类型:(1)联通移动wap(2)电信wap,(3)其他的都是net类型。
还有一些实际项目中需要注意的地方,我已经在代码中写注解,分享给大家。
  1. import com.shoowc.R;   
  2. import android.app.Activity;   
  3. import android.content.Context;   
  4. import android.database.Cursor;   
  5. import android.net.ConnectivityManager;   
  6. import android.net.NetworkInfo;   
  7. import android.net.Uri;   
  8. import android.os.Bundle;   
  9. import android.text.TextUtils;   
  10. import android.util.Log;   
  11.   
  12. public class CheckApnTypeActivity extends Activity {   
  13.   /** Called when the activity is first created. */   
  14.   public static final    String CTWAP = "ctwap";   
  15.   public static final    String CMWAP = "cmwap";   
  16.   public static final    String WAP_3G = "3gwap";   
  17.   public static final    String UNIWAP = "uniwap";   
  18.   public static final    int TYPE_NET_WORK_DISABLED = 0;// 网络不可用   
  19.   public static final    int TYPE_CM_CU_WAP = 4;// 移动联通wap10.0.0.172   
  20.   public static final    int TYPE_CT_WAP = 5;// 电信wap 10.0.0.200   
  21.   public static final    int TYPE_OTHER_NET = 6;// 电信,移动,联通,wifi 等net网络   
  22.   public static Uri PREFERRED_APN_URI = Uri   
  23.   .parse("content://telephony/carriers/preferapn");   
  24.       
  25.       
  26.   @Override   
  27.   public void onCreate(Bundle savedInstanceState) {   
  28.     super.onCreate(savedInstanceState);   
  29.     setContentView(R.layout.main);   
  30.   
  31.     checkNetworkType(this);   
  32.   }   
  33.   
  34.   /***  
  35.     * 判断Network具体类型(联通移动wap,电信wap,其他net)  
  36.     *     
  37.     * */   
  38.   public static int checkNetworkType(Context mContext) {   
  39.     try {   
  40.       final ConnectivityManager connectivityManager = (ConnectivityManager) mContext   
  41.           .getSystemService(Context.CONNECTIVITY_SERVICE);   
  42.       final NetworkInfo mobNetInfoActivity = connectivityManager   
  43.           .getActiveNetworkInfo();   
  44.       if (mobNetInfoActivity == null || !mobNetInfoActivity.isAvailable()) {   
  45.            
  46.   
  47.         // 注意一:   
  48.         // NetworkInfo 为空或者不可以用的时候正常情况应该是当前没有可用网络,   
  49.         // 但是有些电信机器,仍可以正常联网,   
  50.         // 所以当成net网络处理依然尝试连接网络。   
  51.         // (然后在socket中捕捉异常,进行二次判断与用户提示)。   
  52.            
  53.            
  54.   
  55.         Log.i("""=====================>无网络");   
  56.         return TYPE_OTHER_NET;   
  57.       } else {   
  58.   
  59.         // NetworkInfo不为null开始判断是网络类型   
  60.   
  61.         int netType = mobNetInfoActivity.getType();   
  62.         if (netType == ConnectivityManager.TYPE_WIFI) {   
  63.           // wifi net处理   
  64.           Log.i("""=====================>wifi网络");   
  65.           return TYPE_OTHER_NET;   
  66.         } else if (netType == ConnectivityManager.TYPE_MOBILE) {   
  67.               
  68.   
  69.           // 注意二:   
  70.           // 判断是否电信wap:   
  71.           //不要通过getExtraInfo获取接入点名称来判断类型,   
  72.           // 因为通过目前电信多种机型测试发现接入点名称大都为#777或者null,   
  73.           // 电信机器wap接入点中要比移动联通wap接入点多设置一个用户名和密码,   
  74.           // 所以可以通过这个进行判断!   
  75.   
  76.           final Cursor c = mContext.getContentResolver().query(   
  77.               PREFERRED_APN_URI, nullnullnullnull);   
  78.           if (c != null) {   
  79.             c.moveToFirst();   
  80.             final String user = c.getString(c   
  81.                 .getColumnIndex("user"));   
  82.             if (!TextUtils.isEmpty(user)) {   
  83.               Log.i("",   
  84.                   "=====================>代理:"   
  85.                       + c.getString(c   
  86.                           .getColumnIndex("proxy")));   
  87.               if (user.startsWith(CTWAP)) {   
  88.                 Log.i("""=====================>电信wap网络");   
  89.                 return TYPE_CT_WAP;   
  90.               }   
  91.             }   
  92.           }   
  93.           c.close();   
  94.               
  95.               
  96.           // 注意三:   
  97.           // 判断是移动联通wap:   
  98.           // 其实还有一种方法通过getString(c.getColumnIndex("proxy")获取代理ip   
  99.           //来判断接入点,10.0.0.172就是移动联通wap,10.0.0.200就是电信wap,但在   
  100.           //实际开发中并不是所有机器都能获取到接入点代理信息,例如魅族M9 (2.2)等...   
  101.           // 所以采用getExtraInfo获取接入点名字进行判断   
  102.               
  103.           String netMode = mobNetInfoActivity.getExtraInfo();   
  104.           Log.i("""netMode ================== " + netMode);   
  105.           if (netMode != null) {   
  106.             // 通过apn名称判断是否是联通和移动wap   
  107.             netMode=netMode.toLowerCase();   
  108.             if (netMode.equals(CMWAP) || netMode.equals(WAP_3G)   
  109.                 || netMode.equals(UNIWAP)) {   
  110.               Log.i("""=====================>移动联通wap网络");   
  111.               return TYPE_CM_CU_WAP;   
  112.             }   
  113.           }   
  114.         }   
  115.       }   
  116.     } catch (Exception ex) {   
  117.       ex.printStackTrace();   
  118.       return TYPE_OTHER_NET;   
  119.     }   
  120.   
  121.     return TYPE_OTHER_NET;   
  122.   }   
  123. }  
posted @ 2014-03-10 10:16  huidaoli  阅读(895)  评论(0编辑  收藏  举报