当前位置: 移动技术网 > IT编程>移动开发>Android > Android 系统net和wap接入点的区别

Android 系统net和wap接入点的区别

2019年07月24日  | 移动技术网IT编程  | 我要评论

网游之东汉三国,灵龟之家两爬论坛,天门电影院

      我们使用android设备连接网络时,如果是wap接入点就需要设置代理,而电信和移动联通的代理并不相同,移动和联通的wap代理都是10.0.0.172:80,电信的wap代理是10.0.0.200:80,所以进行android开发时很有必要判断接入点。

       接入点的类型

       net网络:运营商(移动联通电信)net网络,wifi,usb网络共享

       wap网络:移动联通wap(代理相同:10.0.0.172:80),电信wap(代理:10.0.0.200:80)

       这样看来就可以抽象出三种网络类型:联通移动wap,电信wap,其他的都是net类型。

       接入点判断实例

        下面给出了接入点判断的实例代码,其中进行了一些注解,都是一些在实际项目开发中需要注意的地方。分享给大家以供参考。

java代码

package com.johnson.utils;  
import com.shoowc.r;  
import android.app.activity;  
import android.content.context;  
import android.database.cursor;  
import android.net.connectivitymanager;  
import android.net.networkinfo;  
import android.net.uri;  
import android.os.bundle;  
import android.text.textutils;  
import android.util.log;  
 
public class ex01_checkapntypeactivity extends activity {  
 /** called when the activity is first created. */  
 public static final  string ctwap = "ctwap";  
 public static final  string cmwap = "cmwap";  
 public static final  string wap_3g = "3gwap";  
 public static final  string uniwap = "uniwap";  
 public static final  int type_net_work_disabled = 0;// 网络不可用  
 public static final  int type_cm_cu_wap = 4;// 移动联通wap10.0.0.172  
 public static final  int type_ct_wap = 5;// 电信wap 10.0.0.200  
 public static final  int type_other_net = 6;// 电信,移动,联通,wifi 等net网络  
 public static uri preferred_apn_uri = uri  
 .parse("content://telephony/carriers/preferapn");  
 @override  
 public void oncreate(bundle savedinstancestate) {  
  super.oncreate(savedinstancestate);  
  setcontentview(r.layout.main);  
 
  checknetworktype(this);  
 }  
 
 /***  
  * 判断network具体类型(联通移动wap,电信wap,其他net)  
  *   
  * */  
 public static int checknetworktype(context mcontext) {  
  try {  
   final connectivitymanager connectivitymanager = (connectivitymanager) mcontext  
     .getsystemservice(context.connectivity_service);  
   final networkinfo mobnetinfoactivity = connectivitymanager  
     .getactivenetworkinfo();  
   if (mobnetinfoactivity == null || !mobnetinfoactivity.isavailable()) {  
      
 
    // 注意一:  
    // networkinfo 为空或者不可以用的时候正常情况应该是当前没有可用网络,  
    // 但是有些电信机器,仍可以正常联网,  
    // 所以当成net网络处理依然尝试连接网络。  
    // (然后在socket中捕捉异常,进行二次判断与用户提示)。  
      
    log.i("", "=====================>无网络");  
    return type_other_net;  
   } else {  
 
    // networkinfo不为null开始判断是网络类型  
 
    int nettype = mobnetinfoactivity.gettype();  
    if (nettype == connectivitymanager.type_wifi) {  
     // wifi net处理  
     log.i("", "=====================>wifi网络");  
     return type_other_net;  
    } else if (nettype == connectivitymanager.type_mobile) {  
        
 
     // 注意二:  
     // 判断是否电信wap:  
     //不要通过getextrainfo获取接入点名称来判断类型,  
     // 因为通过目前电信多种机型测试发现接入点名称大都为#777或者null,  
     // 电信机器wap接入点中要比移动联通wap接入点多设置一个用户名和密码,  
     // 所以可以通过这个进行判断!  
 
     final cursor c = mcontext.getcontentresolver().query(  
       preferred_apn_uri, null, null, null, null);  
     if (c != null) {  
      c.movetofirst();  
      final string user = c.getstring(c  
        .getcolumnindex("user"));  
      if (!textutils.isempty(user)) {  
       log.i("",  
         "=====================>代理:"  
           + c.getstring(c  
             .getcolumnindex("proxy")));  
       if (user.startswith(ctwap)) {  
        log.i("", "=====================>电信wap网络");  
        return type_ct_wap;  
       }  
      }  
     }  
     c.close();  
        
        
     // 注意三:  
     // 判断是移动联通wap:  
     // 其实还有一种方法通过getstring(c.getcolumnindex("proxy")获取代理ip  
     //来判断接入点,10.0.0.172就是移动联通wap,10.0.0.200就是电信wap,但在  
     //实际开发中并不是所有机器都能获取到接入点代理信息,例如魅族m9 (2.2)等...  
     // 所以采用getextrainfo获取接入点名字进行判断  
        
     string netmode = mobnetinfoactivity.getextrainfo();  
     log.i("", "netmode ================== " + netmode);  
     if (netmode != null) {  
      // 通过apn名称判断是否是联通和移动wap  
      netmode=netmode.tolowercase();  
      if (netmode.equals(cmwap) || netmode.equals(wap_3g)  
        || netmode.equals(uniwap)) {  
       log.i("", "=====================>移动联通wap网络");  
       return type_cm_cu_wap;  
      }  
 
     }  
 
    }  
   }  
  } catch (exception ex) {  
   ex.printstacktrace();  
   return type_other_net;  
  }  
 
  return type_other_net;  
 
 }  
} 

       以上是完整的实例代码,另外附加了详细的注解,相信大家能够看的很明白了。

        通过此文,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网