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秒的时间,仅仅有切换过去,才结束
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有何差别的更多相关文章
- [Java][Android][Process] ProcessBuilder与Runtime差别
在Android中想要进行Ping,在不Root机器的情况下似乎还仅仅能进行底层命调用才干实现. 由于在Java中要进行ICMP包发送须要Root权限. 于是仅仅能通过创建进程来攻克了.创建进程在Ja ...
- Android MarginLeft与MarginStart的差别
在写layout布局的时候,我们会发现有这样几个比較相似的属性: MarginStart MarginLeft MarginEnd MarginRight 这些属性的差别是什么? 依据ap ...
- android:giavity和layout_gravity的差别
android:gravity: 是对该view中内容的限定.比方一个button 上面的text. 你能够设置该text 相对于view的靠左,靠右等位置. android:layout_gravi ...
- Android Message和obtainMessage的差别
前几天须要实现一个以太网功能就看了以太网的源代码部分,看见了源代码部分在消息处理时,发现有一些不同的地方: 平时我在处理消息时: 1.首先创建Handler对象: private Hand ...
- android开发者要懂得问题答案
我在网上看了一下有些人在博客上提出一些什么android开发者必须懂得问题,可是就是没有答案,所以我就把这些问题拷贝过来了.顺便也把全部的答案加上,为了让很多其它的开发者高速的找到答案,谢谢! 以下的 ...
- android一些面试题目
1.ListView怎么提高滑动效率 2.说下你做过项目的包的构架,(联网,解析,activity,database) 重点 3.载入大量图片怎么做(包含小图和查看大图) 怎么降低一次跟server的 ...
- android 手机网络接入点名称及WAP、NET模式的区别
移动 电信 联通 APN cmwap cmnet ctwap ctnet 3gwap uniwap 3gnet uninet设置 APN(Access Point Name),即“接入点名称”,用来标 ...
- Android Studio使用教程(一)(转)
今年的Google全球开发者大会虽然没有新的Android系统和设备,但是还是推出了一些不错的产品,Android Studio就是其中之一.这个基于Intellij IDEA开发的Android I ...
- [Java][Android][Process] 暴力的服务能够解决一切,暴力的方式运行命令行语句
不管是在Java或者Android中运行命令行语句殊途同归都是创建一个子进程运行调用可运行文件运行命令.类似于Windows中的CMD一样. 此时你有两种方式运行:ProcessBuilder与Run ...
随机推荐
- UVA 10198 Counting
Counting The Problem Gustavo knows how to count, but he is now learning how write numbers. As he is ...
- asp.net中使用forms验证
1.首先在web.config中修改验证方式为 "Forms" <authentication mode="Forms"> 这里的模式有很多中,可自 ...
- Eclipse汉化后怎么改回英文版 (中文 改 英文)
Eclipse汉化后怎么改回英文版(可切换中英文) 很多朋友将MyEclipse汉化后还想改回英文的,其实只要修改MyEclipse的配置文件就可以了,这里我以MyEclipse7.0为例演示一下如何 ...
- 分享一个通用的分页SQL
又很久没写博客,今天记录一个SQLserver通用分页存储过程(适用于SqlServer2000及以上版本) 1.支持连表 2.支持条件查询 USE [MYDB] GO /****** Object: ...
- c#使用Dictionary统计字符串中出现次数最多字符
最近在找工作,遇到这样一道面试题: 对于给定的一个字符串,统计出该串中各个字符出现的次数,并打印出出现次数最多的那个字符 因为本人是个菜鸟,所以当时写的思路是用递归 /*str 字符串, strA 第 ...
- js实现IE/Firefox的收藏功能
代码: function addFavorite(el){ var url = window.location.href, title = $('head').find('title').html() ...
- 使用ES6进行开发的思考
ECMAScript6已经于近日进入了RC阶段,而早在其处于社区讨论时,我就开始一直在尝试使用ES6进行开发的方案.在Babel推出后,基于ES6的开发也有了具体可执行的解决方案,无论是Build还是 ...
- nginx网站架构优化思路(原)
本人接触的优化主要分为三大类 黑体的为本模块下的重点 ---------------安全优化 安全在生产场景中是第一位的 1.1 站点目录权限的优化 (修改权限755 644 所属用户root,需要 ...
- [UVA] 784 - Maze Exploration
Maze Exploration A maze of rectangular rooms is represented on a two dimensional grid as illustra ...
- php----浅谈一下empty isset is_null的用处
} } { } { } } } { } { } is_null():判断变量是否为null if ($a){} 那这个未声明变量会报noti ...