zoukankan      html  css  js  c++  java
  • android CMWAP, CMNET有何差别

    什么是CMNET,什么是CMWAP?
    答:CMWAP和CMNET仅仅是中国移动为其划分的两个GPRS接入方式。中国移动对CMWAP作了一定的限制,主要表如今CMWAP接入时仅仅能訪问GPRS网络内的IP(10.*.*.*),而无法通过路由訪问Internet,我们用CMWAP浏览Internet上的网页就是通过WAP网关协议或它提供的HTTP代理服务实现的。 因此,仅仅有满足下面两个条件的应用才干在中国移动的CMWAP接入方式下正常工作: 
    1.应用程序的网络请求基于HTTP协议。 
    2.应用程序支持HTTP代理协议或WAP网关协议
    。 
    这也就是为什么我们的G1无法正经常使用CMWAP的原因。
    一句话:CMWAP是移动限制的,理论上仅仅能上WAP网,而CMNET能够用GPRS浏览WWW

    首先推断是Wifi还是Mobile,假设是Mobile 有两种,一种是cmwap,还有一种是cmnet。假设是cmwap ,则须要设置代理才干连接。

        ConnectivityManager conManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

           //mobile 3G Data NetworkState

           mobile =conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();

           //wifi State

           wifi =conManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();

           ConnectivityManager conManager =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

           //mobile 3G Data NetworkState

           mobile = conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();

           //wifi State

           wifi = conManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();


    取得网络链接

         ConnectivityManager conManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

           //mobile 3G Data NetworkState

           mobile = conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();

           //wifi State

           wifi = conManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();

           ConnectivityManager conManager= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

           //mobile 3G Data NetworkState

           mobile = conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();

           //wifi State

           wifi = conManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();

     

    有时候须要实现cmwap和cmnet两种接入方式转换,我自己就遇到过这个问题,经过在网上及查找资料,能够通过程序改动apn表,就能够改变接入方式,可是转换结束后,我測试了一下,大概须要6-8秒的时间,网上说是4秒,不知道他们如何測试的,

    改动原理,android将apn保存到数据库表,详细位置,进入android root文件夹,/data/data/com.android.providers.telephony/datebase/telephony.db/carriers  打开后就能够看到里边的值了,这个表中保存着全部手机支持的网络接入方式,不同的生产商,及同一个生产商的不同手机型号,里边的值不一样,只是最主要的几个还是从在的,比方移动,联通等。 表中有个字段为current的,这个值假设为1,说明是当前的链接方式,可是感觉不正确,由于我自己的机子老是有三到四个值为1的,通过分析感觉更像是当前网络能够选择的链接方式,比方我用的是移动的,全部移动的联网方式都为1.,假设有知道的能够分享一下,

    我们改动apn接入方式时,事实上并非改动这个表,这也是这表为什么不保存当前网络连接方式的原因,而真真保存当前网络连接方式的是在xml文件里,位置是:/com.android.providers.telephony/shared_prefs/preferred-apn.xml中,文件的内容大概是<map> <long name="apn_id" value="261"/></map>,apn_id是标签名,values值为上面提到的表的_id。表示当前正在使用这个方式接入网络

    ok,那我们就明确要如何做了吧?首先从表中查找到cmwap相应的id,查询方式一般为like '%cmwap%',找到以后将上面提到的xml文件的values值改动为查找到的就ok了,
    以下是详细实现,(写的时候有几个字段名折腾了好久,感觉不安常规出牌)

      public class UpdateAPN{

            public String current;

            public String name;

            private Uri uriApn;

            private Uri uriNowApn;

            public UpdateAPN() {

               this.uriApn = Uri.parse("content://telephony/carriers");

               this.uriNowApn = Uri.parse("content://telephony/carriers/preferapn");

               this.name = "cmnet";

               this.current = "1";

            }

            private String getAPN(){

               String str1 = null;

               ContentResolver localContentResolver = AppInfo.globalAppContext.getContentResolver();

               Cursor localCursor = localContentResolver.query(this.uriApn, null, "apn LIKE'%cmnet%'  ", null, null);

               if (localCursor == null){

                   return null;

               }

               for (localCursor.moveToFirst();!localCursor.isAfterLast(); localCursor.moveToNext()){

                   String apnName = localCursor.getString(localCursor.getColumnIndex("apn")).toLowerCase();

                   if (name.equals(apnName)){

                        int m =localCursor.getColumnIndex("_id");

                        str1 =localCursor.getString(m);

                        return str1;

                   }

               }

               localCursor.close();

               return null;

            }

            private String getNowApn() {

               String str1 = null;

               ContentResolver localContentResolver = AppInfo.globalAppContext.getContentResolver();

               Uri localUri = this.uriNowApn;

               Cursor localCursor = localContentResolver.query(localUri, null, null, null, null);

               while (true) {

                   if ((localCursor == null) ||(!localCursor.moveToNext())) {

                        localCursor.close();

                        return str1;

                   }

                   int i = localCursor.getColumnIndex("_id");

                   str1 = localCursor.getString(i);

                   Constant.debug("getNowApn --> str1=" + str1);

                   return str1;   

               }

            }

            public boolean updateApn(){

               try {

                   String str1 = getAPN();//列表id cmnet

                   String str2 = getNowApn();//当前连接id cmwap         

                   Constant.debug("apn---> " + str1 +" & nowApn ---> " + str2);

                   if (str1.equals(str2))

                        return false;

                   ContentResolver localContentResolver = AppInfo.globalAppContext.getContentResolver();

                   ContentValues localContentValues = new ContentValues();

                   String str3 = getAPN();//列表id cmnet

                   localContentValues.put("apn_id", str3);

                   Uri localUri = this.uriNowApn;

                    int i = localContentResolver.update(localUri,

                    localContentValues, null, null);

                   return true;

               } catch (Exception localException) {

                   String str4 = String.valueOf(localException);

                   int j = Log.v("pleaseset cmwap's apn", str4);

                   return false;

               }

            }

        }

    以下大概測试,
    首先推断网络是否cmwap链接,这个应该都会,所以不说了,
    假设是cmwap,则进行切换,
    切换结束后,然后反复推断网络能否够连接
    NetworkUtils类例如以下

    public class NetworkUtils{

           //cmwap转换cmnet

           public static boolean cmwap2Cmnet(Contextcontext)

                       return new UpdateAPN().updateApn();

                }

           //是否是cmwap网络连接

           public static boolean isCmwap(Contextcontext) {

                          if (context== null) {

                                   return false;

                           }

                           ConnectivityManager cm =(ConnectivityManager) context

                                           .getSystemService(Context.CONNECTIVITY_SERVICE);

                           if (cm == null) {

                                   return false;

                           }

                           NetworkInfo info =cm.getActiveNetworkInfo();

                           if (info ==null) {

                                   return false;

                           }

                           String extraInfo =info.getExtraInfo();

    //                    Constant.debug("extraInfo---> " + extraInfo);

           //工具类,推断是否为空及null

                           if (TextUtils.isEmpty(extraInfo)|| (extraInfo.length() < 3)) {

                                   return false;

                           }

                           if(extraInfo.toLowerCase().indexOf("wap") > 0){

                                   return true;

                           }

                           //return extraInfo.regionMatches(true, extraInfo.length() - 3, "wap",

                           // 0,3);

                           return false;

                   }

           /**

               * 是否是cmnet链接网络

               * @return ture是,false

               */

               public static boolean isCmnet(Context context) {

                   ConnectivityManager cm =(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

                   if (cm == null) {

                       return false;

                   }

                   NetworkInfo info =cm.getActiveNetworkInfo();

                   if (info ==null) {

                       return false;

                   }

                   String extraInfo = info.getExtraInfo();

                   if (TextUtils.isEmpty(extraInfo)|| (extraInfo.length() < 3)) {

                       return false;

                   }

                   if(extraInfo.toLowerCase().indexOf("net") > 0){

                       return true;

                   }

                   return false;

               }

           }

           public void cmwap2Cmnet(){

                   // 假设为cmpwap,启动networkutils是我自己的网络连接工具类,你能够定义为自己的,也能够用方法替换,

                   if(NetworkUtils.isCmwap(this)) {

                       boolean s =NetworkUtils.cmwap2Cmnet(this);

                       if(NetworkUtils.isNetworkAvailable(this)){

                           Constant.debug("切换结束,网络能够连接");

                       }

                       longstartTime = System.currentTimeMillis();

                       int count =0;

                       while(!NetworkUtils.isCmnet(this)) {

                           // cmwap切换到cmnet须要大概4秒的时间,仅仅有切换过去,才结束

                           if (count>= 10) {

                               break;

                           }

                           try{

                               Thread.sleep(1000);

                           }catch(Exception e) {

                           }

                           count++;

                       }

                       long endTime= System.currentTimeMillis();

                       Constant.debug("切换结束,切换花费时间为:" + ((endTime -startTime) / 1000.0) + ";切换循环次数(假设大于10可能没有切换成功):" + count);

                       if(NetworkUtils.isCmnet(this)) {

                           Constant.debug("切换结束,网络连接方式为cmnet");

                       }

                   }



  • 相关阅读:
    项目打包发布到tomcat中,中文出现乱码
    打war包时无法把src/main/java里的xml文件打包上去
    Activemq和Rabbitmq端口冲突
    博客园皮肤炫酷效果
    centos7 ffmpeg安装
    centos7 nginx开启启动
    磁盘满了,找不到占磁盘的文件或者日志
    turn服务部署
    kvm虚拟机配置被克隆rhel6客户机的网卡
    jenkins自动构建
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3820723.html
Copyright © 2011-2022 走看看