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/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, null, null);
if (c != null)
{
/*
* Fields you can retrieve can be found in
com.android.providers.telephony.TelephonyProvider :
db.execSQL("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\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, 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 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'.
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xiechengfa/archive/2011/03/05/6225130.aspx
分享到:
相关推荐
本书《Managing RAID on Linux》由Derek Vadala编写,是一本深入探讨如何在Linux环境下管理和配置RAID系统的专业教材。该书首先介绍了RAID的基本概念和技术背景,然后详细讲解了在Linux系统中实现RAID的各种方法,并...
### 一、Blink:管理间歇性供电下的服务器集群 #### 1.1 背景与动机 随着数据中心的能耗成本不断攀升,成为总体拥有成本(Total Cost of Ownership, TCO)的重要组成部分,降低数据中心能耗的影响已经成为产业界和...
标题《在谷歌寻找构建债务:管理技术债务的经验》和描述提到的关键词包括Google、技术债务、构建系统,描述了谷歌工程师们在庞大且快速变化的代码库中不断偿还各种形式的技术债务的过程。文章还指出,工程师们通过...
配置和管理EMC Celerra上的CIFS是一个深入的主题,它涉及到网络文件服务的操作和管理。EMC Celerra Network Server集成了作为网络文件服务开放标准的CIFS协议。CIFS是为互联网设计的文件访问协议,基于Microsoft ...
Learning Helm Managing Apps on Kubernetes (Matt Butcher, Matt Farina, Josh Dolitsky)
Android Studio 3.2 Development Essentials – Kotlin Edition 版本: Developing Android 9 Apps Using Android Studio 3.2, Kotlin and Android Jetpack By 作者: Neil Smyth ISBN-10 书号: 0960010939 ISBN-13 ...
可扩展的多租户开发 / scalable multi tenant development
Managing Raid On Linux
多文化组织管理,赵劲松,,随着全球化进程的不断推进,来自不同文化背景的职工群体在许多机构中都日渐普遍起来。因此,一个成功的管理者需要对多文化环境带
Using hardware APIs available on Android devices Interacting with other devices via SMS, web browsing, and social networkingStoring data efficiently with SQLite and its alternatives Accessing location...
A succinct, hands-on guide to enhance your game development skills with Android SDK Who This Book Is For If you are an intermediate-level Android developer who wants to create highly interactive and ...
What Does Android Run On? 9 Why Develop for Android? 9 What Will Drive Android Adoption? 10 What Does It Have That Others Don’t? 10 Changing the Mobile Development Landscape 11 Introducing the ...
Rex Black's Managing the Testing Process, a compendium of real-world advice on managing software testing successfully. It is a veritable hodge-podge of sample test documents and is filled with ...
《与熊共舞:软件项目中的风险管理》(Waltzing with Bears Managing Risks On Software Projects)由Tom DeMarco与Timothy Lister合著,该书深入探讨了如何有效地识别并管理软件开发过程中的风险。 ### 为什么要做...
Managing Projects with GNU make, 3rd Edition provides guidelines on meeting the needs of large, modern projects. Also added are a number of interesting advanced topics such as portability, ...