Managing APN settings on Google Android

Source: Internet
Author: User

 

An APN (Access Point name) is the information needed to establish a GPRS/EDGE/UMTS cellular packet data connection on a mobile device. usually the device can be configured by the operator, the OEM, and users with an APN address such as WAP. cingular or epc.tmobile.com
That will be eventually resolved to the IP address of the ggsn in the operator's network.

Users can go to settings --> wireless control --> Mobile Networks --> Access Point names to view and edit existing apns.

Google Android uses a SQLite data table to store all apns configured on the device, as shown below:

Database:/data/COM. Android. providers. telephony/databases/telephony. DB
Table: Carriers
Uri: Content: // telephony/carriers

 

_ Id

Name

Numeric

MCC

MNC

APN

User

Server

Password

1

T-Mobile us

310260

310

260

Epc.tmobile.com

None

*

None

Proxy

Port

Mmsproxy

Mmsport

Mmsc

Type

Current

Http://mms.msg.eng.t-mobile.com/mms/wapenc

Default

1

This is a data record in the carriers table. the "_ id" is the primary key auto-generated when you add new APN records using APIs or the UI manually. the "name" filed will appear on the setting UI. the 'numeric 'field identifies the network that the APN Associates
With, which is a combination of MCC (mobile country code) and MNC (mobile network code ). an operator may have a number of 'numeric 'values to cover all this network. the "mmsproxy", "mmsport", and "mmsc" fields are for MMS configurations. the "type" Field
For an APN can be either 'default' for general data traffic, or 'mms 'for MMS.

Note: Android does not support multiple actively apns (simultaneous PDP contexts), as of 1.6 SDK. In other words, if mms apn is activated, then the default web APN will be disconnected.

The android SDK (1.5 and 1.6) does not provide APIs to manage APN (Access Point name) s directly. so you have to use the Telephony content provider to do that. take a look at the telephonyprovider. JAVA Source
Code will definitely help. I wrote some quick test code to enumerate and add apns to the system, as well as set an APN to be the default one such that the device will use it for subsequent connections (this is indicated by the radio button in the APN list UI ).

  • Enumerate all apns in the system:

/*
* Information of all apns
* Details can be found in COM. Android. providers. telephony. telephonyprovider
*/
Public static final URI apn_table_uri =
Uri. parse ("content: // telephony/carriers ");
/*
* Information of the preferred APN
*
*/
Public static final URI preferred_apn_uri =
Uri. parse ("content: // telephony/carriers/preferapn ");

/*
* Enumerate all APN data
*/
Private void enumerateapns ()
{
Cursor c = context. getcontentresolver (). Query (
Apn_table_uri, null, null );
If (C! = NULL)
{
/*
* Fields you can retrieve can be found in
Com. Android. providers. telephony. telephonyprovider:

Db.exe csql ("create table" + carriers_table +
"(_ Id integer primary key," +
"Name text," +
"Numeric text," +
"MCC text," +
"MNC text," +
"APN text," +
"User text," +
"Server text," +
"Password text," +
"Proxy text," +
"Port text," +
"Mmsproxy text," +
"Mmsport text," +
"Mmsc text," +
"Type text," +
"Current integer );");
*/

String S = "All apns: \ n ";
Log. D (TAG, S );
Try
{
S + = printalldata (c); // print the entire result set
}
Catch (sqlexception E)
{
Log. D (TAG, E. getmessage ());
}

// Log. D (TAG, S + "\ n ");
C. Close ();
}

}

  • Add a new APN record:

/*
* Insert a new APN entry into the system APN table
* Require an APN name, And the APN address. More can be added.
* Return an ID (_ id) that is automatically generated for the new APN entry.
*/
Public int insertapn (string name, string apn_addr)
{
Int id =-1;
Contentresolver resolver = context. getcontentresolver ();
Contentvalues values = new contentvalues ();
Values. Put ("name", name );
Values. Put ("APN", apn_addr );

/*
* The following three field values are for testing in Android emulator only
* The APN setting page UI will only display apns whose 'numeric 'filed is
* Telephonyproperties. property_sim_operator_numeric.
* On Android emulator, this value is 310260, where 310 is MCC, and 260 MNC.
* With these field values, the newly added APN will appear in system UI.
*/
Values. Put ("MCC", "310 ");
Values. Put ("MNC", "260 ");
Values. Put ("numeric", "310260 ");

Cursor c = NULL;
Try
{
Uri newrow = resolver. insert (apn_table_uri, values );
If (newrow! = NULL)
{
C = resolver. Query (newrow, null, null );
Log. D (TAG, "newly added APN :");
Printalldata (c); // print the entire result set

// Obtain the APN ID
Int idindex = C. getcolumnindex ("_ id ");
C. movetofirst ();
Id = C. getshort (idindex );
Log. D (TAG, "new ID:" + ID + ": inserting new APN succeeded! ");
}
}
Catch (sqlexception E)
{
Log. D (TAG, E. getmessage ());
}

If (C! = NULL)
C. Close ();
Return ID;
}

  • Set an APN to be the default

/*
* Set an APN to be the default APN for web traffic
* Require an input of the apn id to be set
*/
Public Boolean setdefaultapn (int id)
{
Boolean res = false;
Contentresolver resolver = context. getcontentresolver ();
Contentvalues values = new contentvalues ();

// See/etc/apns-conf.xml. The telephonyprovider uses this file to provide
// Content: // telephony/carriers/preferapn URI Mapping
Values. Put ("apn_id", ID );
Try
{
Resolver. Update (preferred_apn_uri, values, null, null );
Cursor c = resolver. Query (
Preferred_apn_uri,
New String [] {"name", "APN "},
"_ Id =" + id,
Null,
Null );
If (C! = NULL)
{
Res = true;
C. Close ();
}
}
Catch (sqlexception E)
{
Log. D (TAG, E. getmessage ());
}
Return res;
}

Two helper functions are created to print data using a cursor:

/*
* Return all column names stored in the string array
*/
Private string getallcolumnnames (string [] columnnames)
{
String S = "column names: \ n ";
For (string T: columnnames)
{
S + = T + ": \ t ";
}
Return S + "\ n ";
}

/*
* Print all data records associated with cursor C.
* Return a string that contains all record data.
* For some weird reason, Android SDK log class cannot print very long string message.
* Thus we have to log record-by-record.
*/
Private string printalldata (cursor C)
{
If (C = NULL) return NULL;
String S = "";
Int record_cnt = C. getcolumncount ();
Log. D (TAG, "Total # Of Records:" + record_cnt );

If (C. movetofirst ())
{
String [] columnnames = C. getcolumnnames ();
Log. D (TAG, getallcolumnnames (columnnames ));
S + = getallcolumnnames (columnnames );
Do {
String ROW = "";
For (string columnindex: columnnames)
{
Int I = C. getcolumnindex (columnindex );
Row + = C. getstring (I) + ": \ t ";
}
Row + = "\ n ";
Log. D (TAG, row );
S + = row;
} While (C. movetonext ());
Log. D (TAG, "end of Records ");
}
Return S;
}

The android emulator's default APN is a T-Mobile APN as shown in the picture below:

Then, let's add a new APN and set it to default:

// Let's try to insert a new APN, whose name is 'google2' and APN address is Google.com, just for fun.
Int id = insertapn ("google2", "Google.com ");

// Set the newly added APN to be the default one for web traffic.
// The new one will show up in settings-> wireless controls-> mobile networks-> Access Point names ),
// And has been set as default (indicated by the Green check button)
Setdefaultapn (ID );

Then the newly added APN will appear in the UI and shown as 'default '.

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.