原文地址:http://hi.baidu.com/xiechengfa/blog/item/64fbed801e1b57b06c81191f.html

Managing APN Settings on Google Android

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:

Android 系统 设置 之 网络 APN (一)

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 (一)

更多相关文章

  1. taintDroid系统性能测试之——Android AVD命令行使用
  2. android调用系统的分享接口
  3. 升级android studio后,打包的apk无法访问网络
  4. Android中判断网络连接是否可用
  5. 【Android】判断某个AP是否在系统中存在(PackageManager与Package
  6. 《Android系统学习》第十章:Android消息处理、消息循环和消息队列
  7. 查看Android系统信息的项目
  8. android 使用Java自带的HttpURLConnection 连接网络 读取返回数

随机推荐

  1. php_mvc实现步骤六
  2. Android-Json到arraylist,org.json.JSONEx
  3. 找出数组中大于或等于N的数
  4. 对于PHP中enum的好奇
  5. redis界面管理工具phpRedisAdmin 安装
  6. 分支复位组的反向引用
  7. php不重新编译添加模块 php不重新编译添
  8. PHP万能密码登陆
  9. php 7-177之间能被7整除的总和的平均数
  10. php如何跨站抓取别的站点的页面的补充