什么是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秒的时间,仅仅有切换过去,才结束

if (count>= 10) {

break;

}

try{

Thread.sleep(1000);

}catch(Exception e) {

}

count++;

}

long endTime= System.currentTimeMillis();

Constant.debug("切换结束,切换花费时间为:" + ((endTime -startTime) / 1000.0) +
可能没有切换成功):" + count);

if(NetworkUtils.isCmnet(this)) {

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

}

}

android CMWAP, CMNET有何差别的更多相关文章

  1. [Java][Android][Process] ProcessBuilder与Runtime差别

    在Android中想要进行Ping,在不Root机器的情况下似乎还仅仅能进行底层命调用才干实现. 由于在Java中要进行ICMP包发送须要Root权限. 于是仅仅能通过创建进程来攻克了.创建进程在Ja ...

  2. Android MarginLeft与MarginStart的差别

    在写layout布局的时候,我们会发现有这样几个比較相似的属性: MarginStart   MarginLeft MarginEnd    MarginRight 这些属性的差别是什么?  依据ap ...

  3. android:giavity和layout_gravity的差别

    android:gravity: 是对该view中内容的限定.比方一个button 上面的text. 你能够设置该text 相对于view的靠左,靠右等位置. android:layout_gravi ...

  4. Android Message和obtainMessage的差别

    前几天须要实现一个以太网功能就看了以太网的源代码部分,看见了源代码部分在消息处理时,发现有一些不同的地方:   平时我在处理消息时:   1.首先创建Handler对象:   private Hand ...

  5. android开发者要懂得问题答案

    我在网上看了一下有些人在博客上提出一些什么android开发者必须懂得问题,可是就是没有答案,所以我就把这些问题拷贝过来了.顺便也把全部的答案加上,为了让很多其它的开发者高速的找到答案,谢谢! 以下的 ...

  6. android一些面试题目

    1.ListView怎么提高滑动效率 2.说下你做过项目的包的构架,(联网,解析,activity,database) 重点 3.载入大量图片怎么做(包含小图和查看大图) 怎么降低一次跟server的 ...

  7. android 手机网络接入点名称及WAP、NET模式的区别

    移动 电信 联通 APN cmwap cmnet ctwap ctnet 3gwap uniwap 3gnet uninet设置 APN(Access Point Name),即“接入点名称”,用来标 ...

  8. Android Studio使用教程(一)(转)

    今年的Google全球开发者大会虽然没有新的Android系统和设备,但是还是推出了一些不错的产品,Android Studio就是其中之一.这个基于Intellij IDEA开发的Android I ...

  9. [Java][Android][Process] 暴力的服务能够解决一切,暴力的方式运行命令行语句

    不管是在Java或者Android中运行命令行语句殊途同归都是创建一个子进程运行调用可运行文件运行命令.类似于Windows中的CMD一样. 此时你有两种方式运行:ProcessBuilder与Run ...

随机推荐

  1. IOS的一个带动画的多项选择的控件(一)

    先上效果图: 这个程序分2个层次,一个是顶部的带UITextField的bar,一个是下拉选择的view,下拉选择的view带有4个自己定义的UIView 我们先定义一个UIViewControlle ...

  2. C-二维数组,多维数组

    -----二维数组      ->在数组定义当中,行数和列数需要用常量定义      ->在定义的时候如果没有数值进行填充,则补零      ->第一个数是行,第二个数是列     ...

  3. Javascript实现表格行排序

    网站开发中凡是用到表格来展示数据的,往往都要根据某个列来对行排序,下面是我从书上看到的一个行排序例子,看过后受益匪浅,故分享出来. 直接献上完整代码: <!doctype html> &l ...

  4. 你真的了解:IIS连接数、IIS并发连接数、IIS最大并发工作线程数、应用程序池的队列长度、应用程序池的最大工作进程数 吗?

    原文链接:http://www.cnblogs.com/yinhaichao/p/4060209.html?utm_source=tuicool&utm_medium=referral 一般购 ...

  5. C/C++中的虚析构函数和私有析构函数的使用

    代码: #include <iostream> using namespace std; class A{ public: A(){ cout<<"construct ...

  6. (原)ubuntu16重装显卡驱动后,torch中的问题

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6030232.html 参考网址: https://github.com/torch/cutorch/i ...

  7. Introduction to the POM

    原文:https://maven.apache.org/guides/introduction/introduction-to-the-pom.html Introduction to the POM ...

  8. 网页title标题的闪动效果

    通过网页title来提示用户有新消息这个功能很常见,比如现在的微博,还有一些邮箱,这个功能都很常见. 显示信息数: <input type="text" id="t ...

  9. [转发]Gulp开发教程(翻译)

    Building With Gulp =================== 转载出处 原文地址 翻译出处 对网站资源进行优化,并使用不同浏览器测试并不是网站设计过程中最有意思的部分,但是这个过程中的 ...

  10. Django QuerySets 里的**kwargs: 动态创建ORM查询

    Django的数据库API查询经常包含关键字参数.例如: bob_stories = Story.objects.filter(title_contains='bob', subtitle_conta ...