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 |
|
|
|
|
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:
/*The Android emulator's default APN is a T-Mobile APN as shown in the picture below:
* 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;
}
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'.
相关推荐
Android扫盲5之设置个性短信和来电铃声 Android扫盲6之Android如何切换为U盘 Android扫盲7之彩信图片的存储 Android扫盲8之怎样通过蓝牙传输文件 Android扫盲9之减少流量的几个技巧 Android扫盲10之如何为软件...
大家是不是愁新买的android手机上不了网?有了它就简单了,首先打开-快捷设置-高级设置-还原接入点-就可以了。如果不上网了打开-节电管理-节电优化-移动网络(APN)勾去掉就好了
|--网络之设置apn |--网络图片查看器 |--网络图片的下载与缓存 |--网络文件的下载与关联进度条 |--联系人之各种查找 |--联系人读写与批量操作 |--联系人读取 |--自定义shape形状颜色渐变资源 |--自定义Toast |--...
安卓调谐器3C Toolbox(Android Tuner)是款应用合集,合并了电量显示器小部件、系统调整和多任务切换小部件等。 功能说明: - 支持Android 4.1.x Logcat 阅读器,自动备份的已安装的应用程序 - 可设置自动终止应用...
Java版水果管理系统源码 HopesUtils,安卓常见问题处理集合及工具类 --Activity不允许横竖屏切换 |--Activity常用小技巧 |--Activity按返回直接回到桌面 ...设置apn |--android 调节屏幕亮度 |--android 资
安卓调谐器Android Tuner:功能很强大的一个东西,只是大家使用的时候要注意,很多关键系统功能很有可能被误操作改掉了。 安卓调谐器说明: 安卓调谐器Android Tuner是一款应用合集,合并了Battery 显示器 ...
流量开关,防止偷流量。一般情况下建议用开关关闭流量,防止android系统内置程序访问数据网络,不影响电话、短信
◊所有的Android应用程序或全部进程,电池,CPU,网络,内存 ◊组件状态(WiFi,蓝牙,GPS,3G,电话,APN) ►状态栏通知 ◊电池,CPU,内存,SD,温度可选配下拉图形 ◊在顶部或底部添加电池剩余条 ◊取出原装电池...
A7670C 内置了多种网络协议,支持多种主流软件操作系统的驱动 (Windows,Linux和Android等操作系统主流版本的USB驱动) 和软件功能。同时也集成了主流的工业标准接口,具有强大的扩展能力,包括UART、SPI、12C、GPIO...