android CMWAP, CMNET有何區別

來源:互聯網
上載者:User

什麼是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中,檔案的內容大概是 ,apn_id是標籤名,values值為上面提到的表的_id。表示當前正在使用這個方式接入網路

ok,那我們就明白要怎樣做了吧?首先從表中尋找到cmwap對應的id,查詢方式一般為like '%cmwap%',找到以後將上面提到的xml檔案的values值修改為尋找到的就ok了,
下面是具體實現,(寫的時候有幾個欄位名折騰了好久,感覺不安常規出牌)

publicclass UpdateAPN{

public Stringcurrent;

public Stringname;

privateUri uriApn;

privateUri 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){

returnnull;

}

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();

returnnull;

}

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;

}

}

publicboolean updateApn(){

try {

String str1 = getAPN();//列表idcmnet

String str2 = getNowApn();//當前串連idcmwap

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

if (str1.equals(str2))

returnfalse;

ContentResolver localContentResolver =AppInfo.globalAppContext.getContentResolver();

ContentValues localContentValues =new ContentValues();

String str3 = getAPN();//列表idcmnet

localContentValues.put("apn_id", str3);

Uri localUri =this.uriNowApn;

int i = localContentResolver.update(localUri,

localContentValues, null, null);

returntrue;

} catch (Exception localException) {

String str4 = String.valueOf(localException);

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

returnfalse;

}

}

}

下面大概測試,
首先判斷網路是否cmwap連結,這個應該都會,所以不說了,
如果是cmwap,則進行切換,
切換結束後,然後重複判斷網路是否可以串連
NetworkUtils類如下

publicclass NetworkUtils{

//cmwap轉換cmnet

publicstatic booleancmwap2Cmnet(Contextcontext){

returnnew UpdateAPN().updateApn();

}

//是否是cmwap網路連接

publicstatic booleanisCmwap(Contextcontext) {

if (context==null) {

returnfalse;

}

ConnectivityManager cm =(ConnectivityManager) context

.getSystemService(Context.CONNECTIVITY_SERVICE);

if (cm ==null) {

returnfalse;

}

NetworkInfo info =cm.getActiveNetworkInfo();

if (info ==null) {

returnfalse;

}

String extraInfo =info.getExtraInfo();

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

//工具類,判斷是否為空白及null

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

returnfalse;

}

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

returntrue;

}

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

// 0,3);

returnfalse;

}

/**

*是否是cmnet連結網路

*@returnture是,false否

*/

publicstatic booleanisCmnet(Context context) {

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

if (cm ==null) {

returnfalse;

}

NetworkInfo info =cm.getActiveNetworkInfo();

if (info ==null) {

returnfalse;

}

String extraInfo = info.getExtraInfo();

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

returnfalse;

}

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

returntrue;

}

returnfalse;

}

}

publicvoid 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");

}

}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.