Configuration and online processing of the APN for Android 1.5

Source: Internet
Author: User

Mobile Internet access is divided into two methods: wap and net. A net mobile phone will be connected directly to the Internet, while a Proxy gateway will be added in the middle when wap is used. Mobile Unicom is 10.0.0.172 and port 80. For Internet-related code writing, wap and net are different:

Wap is generally like this:

[Java] URL url = new URL ("http: // 10.0.0.172: 80/index.htm ");
 
HttpURLConnection hc = (HttpURLConnection) url. openConnection ();
 
Hc. setRequestProperty ("X-Online-Host", "www.csdn.net ");
URL url = new URL ("http: // 10.0.0.172: 80/index.htm ");

HttpURLConnection hc = (HttpURLConnection) url. openConnection ();

Hc. setRequestProperty ("X-Online-Host", "www.csdn.net ");

 

Net is generally like this:

[Java] URL url = new URL ("http://www.csdn.net/index.htm ");
HttpURLConnection hc = (HttpURLConnection) url. openConnection ();
URL url = new URL ("http://www.csdn.net/index.htm ");
HttpURLConnection hc = (HttpURLConnection) url. openConnection ();

 

Therefore, when writing a program, it is necessary to detect the current APN type and determine whether it is a wap or net method. Sometimes it is necessary to modify the current APN.

 

Check the current APN:


You can call ContentResolver to obtain all APN IDs. The uri address is "content: // telephony/carriers ". The Code is as follows:

[Java] Uri uri = Uri. parse ("content: // telephony/carriers ");
Cursor cr = getContentResolver (). query (uri, null );
While (cr! = Null & cr. moveToNext ()){
// APN id
String id = cr. getString (cr. getColumnIndex ("_ id "));
// APN name
String apn = cr. getString (cr. getColumnIndex ("apn "));
// Do other things...
}
Uri uri = Uri. parse ("content: // telephony/carriers ");
Cursor cr = getContentResolver (). query (uri, null );
While (cr! = Null & cr. moveToNext ()){
// APN id
String id = cr. getString (cr. getColumnIndex ("_ id "));
// APN name
String apn = cr. getString (cr. getColumnIndex ("apn "));
// Do other things...
}

What is _ id and apn in it? This is a field in the database where the system stores the apn. The system stores all the apns in the database, where the database is/data/com. android. providers. telephony/databases/telephony. db. Connect your G3 to your computer and run the adb command:

Adb pull/data/com. android. providers. telephony/databases/telephony. db f :/

Take a look at it. (There is also an mmssms. db in the same directory, which is the database for storing text messages)

 

 

There are more than 200 apn entries. Only those with the current value of 1 are displayed in the apn settings of the mobile phone. The fields in the Database correspond to the items in the system settings. In the code above, cr. getString (cr. getColumnIndex ("_ id") is used to retrieve an apn _ id. In the same way, other required fields can be retrieved.

However, this is useless. We want the apn currently in use.

Obtain the uri address of the currently used apn: "content: // telephony/carriers/preferapn ". The Code is the same as above. After the uri is replaced, only one of them is obtained. This is the apn currently used, this is the apn that is selected by the small circle behind the apn list in the system settings.

This apn system is saved in an xml file at/data/com. android. providers. telephony/shared_prefs/preferred-apn.xml. You can also take out the file and open it. The content is very simple:

<? Xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>

<Map>
<Long name = "apn_id" value = "218" type = "regxph" text = "yourobjectname"/>
</Map>
That is to say, the apn set to the _ id 218 in the database now.
To determine whether this apn is wap or net, it is best to check whether the proxy is 10.0.0.172. Because the apn field can be modified at will, you may enter the apn field as needed.
By the way, the operation-related code of apn is available in packages/providers/TelephonyProvider/src/com/android/providers/telephony/TelephonyProvider. java in the android source code.
Modify the current APN:
Then modify it. Why? It is possible that a user's card can only access the Internet through wap, But he sets net. If the program detects net but cannot connect to the Internet, change the system settings to wap and try again.
[Java] Uri uri = Uri. parse ("content: // telephony/carriers/preferapn ");
ContentResolver resolver = getContentResolver ();
ContentValues values = new ContentValues ();
Values. put ("apn_id", id );
Resolver. update (uri, values, null, null );
Uri uri = Uri. parse ("content: // telephony/carriers/preferapn ");
ContentResolver resolver = getContentResolver ();
ContentValues values = new ContentValues ();
Values. put ("apn_id", id );
Resolver. update (uri, values, null, null );
Here, the id corresponds to the _ id field in the database. As for how to obtain the net APN, I can see that the current in all the APN is 1, and then I can determine it myself, or else I can create a new apn myself.
For which apn has the current value of 1, the system should determine the Country Code and network code, that is, the MCC and MNC, which are consistent with the current network for display. (The source code is too lazy to read)

Author: liujian885

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.