android CMWAP, CMNET有何区别 不念不忘少年蓝@ 2022-08-27 01:26 120阅读 0赞 什么是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("切换结束,网络可以连接"); \} **long**startTime = 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"); \} \}
还没有评论,来说两句吧...