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 手机中的 APN 设置是指在 Android 手机中设置移动网络的访问点名称,以便手机可以连接移动网络并进行上网、发送 MMS 等操作。 Android 手机 APN 设置的步骤: 1. 打开主菜单并点击“Settings”图标,在...
Android 系统中对于 APN 的网络 API 没有公开,但是我们可以通过阅读源代码,然后进行数据库操作,系统会自动监听数据库的变化,从而实现开启或者关闭 APN。 APN(Access Point Name)是移动网络上的一个概念,指的...
在Android系统中,APN(Access Point Name)是用于配置移动数据网络连接的重要设置,它定义了设备如何连接到互联网,特别是通过蜂窝数据。APN设置包括运营商、用户名、密码、代理服务器、端口等信息,对于使用特定...
在Android系统中,APN(Access Point Name)是设置数据连接的关键配置,它定义了设备如何连接到互联网或移动数据网络。APN包含了运营商提供的网络接入点信息,比如网络类型(2G、3G、4G、5G)、用户名、密码、服务器...
在Android系统中,APN(Access Point Name)是用于设置移动数据网络连接的关键参数,它定义了设备如何连接到互联网,通常包括网络运营商、数据服务类型等信息。本模块主要探讨的是如何在Android应用程序中通过代码来...
总之,"MyApnDemo"项目提供了一个学习和实践Android APN开发的平台,帮助开发者理解和掌握如何在Android系统中控制网络连接的配置,这对于开发涉及网络通信的应用尤其有用。通过深入理解这一过程,开发者可以创建出...
在Android系统中,APN(Access Point Name,接入点名称)是用于移动设备连接网络的重要配置,它定义了手机如何连接到互联网,包括使用的网络类型(如GPRS、3G、4G或5G)、数据服务提供商以及相关认证信息。...
当Android手机出现无法修改或保存APN设置的情况时,原因往往在于系统默认的APN配置与目标网络环境存在差异。例如,当使用了国外运营商定制的ROM时,其内置的APN配置可能与国内运营商的要求不符,导致用户自定义的APN...
本教程将聚焦于创建一个GRRS/3G(GPRS/EDGE和3G网络)APN(Access Point Name,接入点名称)上网开关的Android小部件。这个小部件允许用户快速切换移动数据连接,对于经常需要控制数据流量的用户非常实用。 首先,...
### Android APN开发流程详解 #### 一、数据连接机制概览 Android设备的数据连接...理解这些细节对于深入掌握Android系统的数据连接机制至关重要,有助于开发者优化网络性能、解决连接问题以及开发更稳定的应用程序。
在Android系统中,用户可以通过设置手动添加或修改APN来适应不同运营商的网络需求。然而,Windows Phone 8系统的APN配置方式与Android略有区别,且不支持直接导入APN文件,通常需要用户逐个输入相关参数。 标题所...
在Android系统中,APN设置用于定义数据连接的类型、网络速度、安全设置等,比如2G、3G、4G或5G的数据连接,以及是否启用彩信和互联网服务。在某些情况下,用户可能需要手动切换APN以优化网络性能或者解决特定服务的...
2. **利用pppd完成拨号连接**:pppd(Post-PPP Dialup)是PPP的一个扩展,它可以在Android系统中用于通过数据端口完成实际的拨号连接。 #### 二、DataConnectionTracker的作用 **DataConnectionTracker**是Android中...
4. **APN(接入点名称)**:虽然APN主要用于设置移动数据连接,但有时也可以作为辅助信息帮助定位,例如通过判断用户使用的运营商和网络类型。 5. **网络定位(Network Provider)**:结合基站和WiFi信息进行定位,...
在 Android 系统中,APN(Access Point Name)是手机上网时必须配置的一个参数,它决定了手机通过哪种接入方式来访问网络。APN 保存在数据库中,数据库绝对路径为 `/data/data/...
本项目中,我们关注的重点是如何在Android系统中设置APN,并且实现APN节点的切换,这对于网络服务的提供,特别是在企业级项目中,具有重要的实用价值。 首先,我们要理解APN的作用。APN包含了运营商信息、数据类型...
在Android系统中,APN设置允许用户自定义网络参数,如运营商、数据类型、代理服务器等,这对于控制移动数据的使用和优化网络性能至关重要。 APN的修改方法主要涉及以下几个步骤: 1. **获取设备root权限**:要修改...
APN(Access Point Name)是Android系统中用于设置移动数据连接的重要配置,它定义了设备如何连接到网络,包括运营商的接入点、用户名、密码等信息。在进行网络调试或优化时,APN参数的管理和比对是一项必不可少的...
在Android系统中,APN(Access Point Name)是用于配置移动设备通过哪种网络连接到互联网的关键设置。APN包含了运营商信息、数据计划类型、代理服务器等参数,通常由运营商预设,但有时开发者可能需要自定义APN以...
综上所述,Android开发中判断网络状态、获取网络运营商和网络类型以及打开网络设置接口的方法都是通过系统提供的服务和类来实现的,它们是构建网络相关应用不可或缺的基础。开发者应熟练掌握这些技能,以确保应用在...