什么是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. Opencv 简单的图片显示

    #include <opencv\cv.h> #include <opencv\highgui.h> #include <opencv\cxcore.h> int ...

  2. 多路复用I/O poll()

    1.基本知识 poll的机制与select类似,与select在本质上没有多大差别,管理多个描述符也是进行轮询,根据描述符的状态进行处理,但是poll没有最大文件描述符数量的限制.poll和selec ...

  3. Android Notification使用及取消

    //发送通知 NotificationManager manger = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE ...

  4. ibatis之##与$$的 使用

    /** 主要讲一下ibatis中$$的使用: 是为了传递参数; 参数一定在Action层用''包裹起来: */ List <SysRole> userList= systemService ...

  5. 用JS的for循环打印九九乘法表

    需要使用两个for循环嵌套,代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  6. 理清fineuploader无刷新上传的一些事

    1.fineuploader是一款不依赖与jquery的异步无刷新上传组件,fineuploader采用ajax方式实现对文件上传,返回值都是以json的格式,对后台服务器操作和前端dom对象一些操作 ...

  7. EF中的TPH、TPT、TPC

    1. Table Per Hierarchy(TPH):只建立一个表,把基类和子类中的所有属性都映射为表中的列2. Table Per Type(TPT):为基类和每个子类建立一个表,每个与子类对应的 ...

  8. ado.net数据库操作(1)

    这些都是网上搜索到的,我把他放在自己的博客里,作为笔记 1.1创建数据库连接(sqlserver) <%@ Import Namespace="System.Data" %& ...

  9. WARNING: The following packages cannot be authenticated! foo bar baz Install these packages without verification [y/N]?

    apt-get install debian-archive-keyring apt-get update

  10. Oracle复杂查询

    1:列出所有员工的姓名,部门名称,和工资 select a1.ename,a1.sal,a2.dname from emp a1,dept a2 where a1.deptno = a2.deptno ...