What is the difference between android CMWAP and CMNET?

Source: Internet
Author: User

What is CMNET and CMWAP?
A: CMWAP and CMNET are only two GPRS access methods for China Mobile. China Mobile imposes limits on CMWAP, mainly because CMWAP can only access IP addresses in the GPRS network (10. *. *. *), but cannot access the Internet through routing. We use CMWAP to browse the Web pages on the Internet through the WAP Gateway Protocol or the HTTP proxy service provided by it. Therefore, only applications that meet the following conditions can work normally in China Mobile's CMWAP access mode:
1. The network request of the application is based on the HTTP protocol.
2. Applications Support HTTP Proxy protocol or WAP gateway protocol.
This is why our G1 cannot use CMWAP normally.
One sentence: CMWAP is a mobile restriction. Theoretically, it can only be connected to the WAP network, while CMNET can use GPRS to browse WWW.

First, determine whether it is Wifi or Mobile. If it is Mobile, there are two types: cmwap and cmnet. If it is cmwap, you need to set a proxy to connect.

ConnectivityManager conManager = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);

// Mobile 3G Data NetworkState

Mobile= ConManager. getNetworkInfo (ConnectivityManager.TYPE_MOBILE). GetState ();

//WifiState

Wifi= ConManager. getNetworkInfo (ConnectivityManager.TYPE_WIFI). GetState ();

ConnectivityManagerConManager= (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);

// Mobile 3G Data NetworkState

Mobile= ConManager. getNetworkInfo (ConnectivityManager.TYPE_MOBILE). GetState ();

//WifiState

Wifi= ConManager. getNetworkInfo (ConnectivityManager.TYPE_WIFI). GetState ();


Get network connection

ConnectivityManagerConManager = (ConnectivityManager) GetSystemService (Context. CONNECTIVITY_SERVICE );

// Mobile 3G Data NetworkState

Mobile= ConManager. getNetworkInfo (ConnectivityManager. TYPE_MOBILE). getState ();

//WifiState

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

//WifiState

Wifi= ConManager. getNetworkInfo (ConnectivityManager. TYPE_WIFI). getState ();

Sometimes we need to implement the conversion of cmwap and cmnet access methods. I have encountered this problem myself. After going online and searching for information, I can modify the apn table through a program to change the access method, however, after the switch, I tested it. It took about 6 to 8 seconds. I said it was 4 seconds on the Internet. I don't know how they tested it,

Modify the principle. android saves the apn to the database table. Go to the android root directory,/data/com. android. providers. telephony/datebase/telephony. after the db/carriers is turned on, you can see the value inside. This table stores the network access methods supported by all mobile phones, different manufacturers, and different mobile phone models of the same manufacturer, the values inside are different, but the most basic ones are from the same, such as mobile and Unicom. There is a field in the table that is current. If this value is 1, it indicates the current link method, but it does not feel correct, because my own machine always has three to four values of 1, the analysis is more like the link method that can be selected on the current network. For example, if I use mobile, all mobile networking methods are 1 ., if you know something, share it with us,

This table is not modified when we modify the apn access mode. This is why this table does not save the current network connection mode, the current network connection method is saved in the xml file at/com. android. providers. in telephony/shared_prefs/preferred-apn.xml, the file content is probably , Apn_id is the label name, and the value of values is the _ id of the table mentioned above. Indicates that the network is being connected using this method.

OK, then we will understand how to do it? First, find the id of cmwap from the table. The query method is like '% cmwap % ', after finding it, change the values value of the xml file mentioned above to the one found,
The following is the specific implementation. (when I write a few field names, I feel uneasy about playing cards)

PublicclassUpdateAPN {

PublicStringcurrent;

PublicStringname;

PrivateUriUriApn;

PrivateUriUriNowApn;

PublicUpdateAPN (){

This. UriApn=Uri. Parse ("content: // telephony/carriers ");

This. UriNowApn=Uri. Parse ("content: // telephony/carriers/preferapn ");

This. Name = "cmnet ";

This. Current = "1 ";

}

PrivateString getAPN (){

String str1 =Null;

ContentResolverLocalContentResolver =AppInfo. GlobalAppContext. getContentResolver ();

CursorLocalCursor = 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 )){

IntM = localCursor. getColumnIndex ("_ id ");

Str1 = localCursor. getString (m );

ReturnStr1;

}

}

LocalCursor. close ();

Returnnull;

}

PrivateString getNowApn (){

String str1 =Null;

ContentResolverLocalContentResolver =AppInfo. GlobalAppContext. getContentResolver ();

UriLocalUri =This. UriNowApn;

CursorLocalCursor = localContentResolver. query (localUri,Null,Null,Null,Null);

While(True){

If(LocalCursor =Null) | (! LocalCursor. moveToNext ())){

LocalCursor. close ();

ReturnStr1;

}

IntI = localCursor. getColumnIndex ("_ id ");

Str1 = localCursor. getString (I );

Constant. Debug ("getNowApn --> str1 =" + str1 );

ReturnStr1;

}

}

PublicbooleanUpdateApn (){

Try{

String str1 = getAPN (); // list idCmnet

String str2 = getNowApn (); // current connection idCmwap

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

If(Str1.equals (str2 ))

Returnfalse;

ContentResolverLocalContentResolver =AppInfo. GlobalAppContext. getContentResolver ();

ContentValuesLocalContentValues =New ContentValues();

String str3 = getAPN (); // list idCmnet

LocalContentValues. put ("apn_id", str3 );

UriLocalUri =This. UriNowApn;

IntI = localContentResolver. update (localUri,

LocalContentValues,Null,Null);

Returntrue;

}Catch(Exception localException ){

String str4 = String.ValueOf(LocalException );

IntJ =Log. V ("pleaseset cmwap's apn", str4 );

Returnfalse;

}

}

}

Below is a rough test,
First, determine whether the network is connected to cmwap,
For cmwap, switch,
After switching, repeat to determine whether the network can be connected.
The NetworkUtils class is as follows:

PublicclassNetworkUtils {

//CmwapConversionCmnet

Publicstatic booleanCmwap2Cmnet (Contextcontext){

Returnnew UpdateAPN(). UpdateApn ();

}

// Whether it isCmwapNetwork Connection

Publicstatic booleanIsCmwap (Contextcontext){

If(Context =Null){

Returnfalse;

}

ConnectivityManager cm = (ConnectivityManager) context

. GetSystemService (Context.CONNECTIVITY_SERVICE);

If(Cm =Null){

Returnfalse;

}

NetworkInfoInfo = cm. getActiveNetworkInfo ();

If(Info =Null){

Returnfalse;

}

String extraInfo = info. getExtraInfo ();

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

// Tool class to determine whether it is null or not

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;

}

/**

* Whether it isCmnetLinked network

*@ ReturnTureYes, false No

*/

Publicstatic booleanIsCmnet (Context context){

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

If(Cm =Null){

Returnfalse;

}

NetworkInfoInfo = 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;

}

}

PublicvoidCmwap2Cmnet (){

// IfCmpwap, StartNetworkutilsIt is my own Network Connection Tool class. You can define it as your own or replace it with a method,

If(NetworkUtils. isCmwap (This)){

BooleanS = NetworkUtils. cmwap2Cmnet (This);

If(NetworkUtils.IsNetworkAvailable(This)){

Constant. Debug ("the switchover is over and the network can be connected ");

}

LongStartTime = System. currentTimeMillis ();

IntCount = 0;

While(! NetworkUtils. isCmnet (This)){

//CmwapSwitchCmnetIt takes about 4 seconds to complete.

If(Count> = 10 ){

Break;

}

Try{

Thread. sleep (1000 );

}Catch(Exception e ){

}

Count ++;

}

LongEndTime = System. currentTimeMillis ();

Constant. Debug ("the switch ends. The switching duration is:" + (endTime-startTime)/1000.0) + "; the number of switching cycles (if it is greater than 10, the switch may not be successful ): "+ count );

If(NetworkUtils. isCmnet (This)){

Constant. Debug ("the switchover is over and the network connection mode is cmnet ");

}

}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.