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]
View plaincopyprint?
  1. URL url = new URL ("http: // 10.0.0.172: 80/index.htm ");
  2. Httpurlconnection Hc = (httpurlconnection) URL. openconnection ();
  3. HC. setrequestproperty ("X-Online-host", "www.csdn.net ");

URL url = new URL ("http: // 10.0.0.172: 80/index.htm"); </P> <p> httpurlconnection Hc = (httpurlconnection) URL. openconnection (); </P> <p> HC. setrequestproperty ("X-Online-host", "www.csdn.net"); </P> <p>

Net is generally like this:

[Java]
View plaincopyprint?
  1. URL url = new URL ("http://www.csdn.net/index.htm ");
  2. Httpurlconnection Hc = (httpurlconnection) URL. openconnection ();

URL url = new URL ("http://www.csdn.net/index.htm"); <br/> 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]
View plaincopyprint?
  1. Uri uri = URI. parse ("content: // telephony/carriers ");
  2. Cursor Cr = getcontentresolver (). Query (Uri, null );
  3. While (CR! = NULL & Cr. movetonext ()){
  4. // APN ID
  5. String id = Cr. getstring (Cr. getcolumnindex ("_ id "));
  6. // APN name
  7. String APN = Cr. getstring (Cr. getcolumnindex ("APN "));
  8. // Do other things...
  9. }

Uri uri = Uri. parse ("content: // telephony/carriers"); <br/> cursor Cr = getcontentresolver (). query (Uri, null, null); <br/> while (CR! = NULL & Cr. movetonext () {<br/> // APN id <br/> string id = CR. getstring (Cr. getcolumnindex ("_ id"); <br/> // APN name <br/> string APN = CR. getstring (Cr. getcolumnindex ("APN"); <br/> // do other things... <br/>}

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"
/>  
</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]
View plaincopyprint?
  1. Uri uri = URI. parse ("content: // telephony/carriers/preferapn ");
  2. Contentresolver resolver = getcontentresolver ();
  3. Contentvalues values = new contentvalues ();
  4. Values. Put ("apn_id", ID );
  5. Resolver. Update (Uri, values, null, null );

Uri uri = Uri. parse ("content: // telephony/carriers/preferapn"); <br/> contentresolver resolver = getcontentresolver (); <br/> contentvalues values = new contentvalues (); <br/> values. put ("apn_id", ID); <br/> 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)

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.