Android 系统APN配置具体解释

这些天一直在调系统原生的Settings.apk里面APN配置的问题。在设置里面手动添加了APN配置选项。可是在界面上还是看不到。所以跟了下代码。原以为就是简单的页面显示的问题。这一跟不要紧。一下就快追到HAL层去了(NND).

首先看Settings.apk的源代码,位于packages/apps/Settings/src/com/android/settings/文件夹下:首先找到ApnSettings类。继承于PreferenceActivity,并实现了Preference.OnPreferenceChangeListener接口。PreferencesActivity是Android中专门用来实现程序设置界面及參数存储的一个Activity。这里就不再赘述了。

public class ApnSettings extends PreferenceActivity implements
Preference.OnPreferenceChangeListener { // 恢复出厂设置的URI
public static final String RESTORE_CARRIERS_URI = "content://telephony/carriers/restore";
// 普通URI。用于ContentPrivoder保存着APN配置信息
public static final String PREFERRED_APN_URI = "content://telephony/carriers/preferapn"; private static final Uri DEFAULTAPN_URI = Uri.parse(RESTORE_CARRIERS_URI);
private static final Uri PREFERAPN_URI = Uri.parse(PREFERRED_APN_URI); // 两个句柄。用于恢复出厂设置
private RestoreApnUiHandler mRestoreApnUiHandler;
private RestoreApnProcessHandler mRestoreApnProcessHandler; private String mSelectedKey; // 组播接收的Intent过滤器
private IntentFilter mMobileStateFilter; private final BroadcastReceiver mMobileStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
Phone.DataState state = getMobileDataState(intent);
switch (state) {
case CONNECTED:
if (!mRestoreDefaultApnMode) {
fillList();
} else {
showDialog(DIALOG_RESTORE_DEFAULTAPN);
}
break;
}
}
}
}; @Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
// 在activity创建的时候依据xml文件来配置视图。
// 实际上 res/xml/apn_settings.xml这个文件就是一个空的PreferenceScreen
addPreferencesFromResource(R.xml.apn_settings);
getListView().setItemsCanFocus(true); // 假设有List则获得焦点
// 这个创建一个Inter 过滤器。过滤的动作为 ACTION_ANY_DATA_CONNECTION_STATE_CHANGED
mMobileStateFilter = new IntentFilter(
TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
} @Override
protected void onResume() {
super.onResume();
// 注冊一个广播接受者
registerReceiver(mMobileStateReceiver, mMobileStateFilter);
if (!mRestoreDefaultApnMode) { // 假设不是恢复出厂设置
fillList(); // 填充Activity的ListView
} else {
showDialog(DIALOG_RESTORE_DEFAULTAPN);
}
}
}

1)    这里首先在onCreate()方法中,依据apn_settings.xml文件来配置界面的视图。实际上就是一个PreferenceScreen。创建一个Intent过滤器, 过滤动作为ACTION_ANY_DATA_CONNECTION_STATE_CHANGED。

2)    然后在onResume()方法中,注冊一个广播接受者。当收到上面的ACTION_ANY_DATA_CONNECTION_STATE_CHANGED动作时。调用

mMobileStateReceiver的onReceive()方法。其目的是为了,当我们进入APN设置的时候,这是再插上SIM卡能显示出APN的配置信息。然后推断是不是须要恢复出厂设置,假设不是。则调用fillList()方法填充当前Activity,显示出APN的配置信息。

3)   首先获取系统属性gsm.sim.operator.numeric,依据这个參数通过系统提供的ContentProvider查询数据库(位于/data/data/com.android.providers.Telephony下的telephony.db数据库中carriers表中)。获得相应的配置信息。然后将其填充到每个ApnPreference中,最后将每个ApnPreference显示到当前的PreferenceGroup上。

private void fillList() {
// 获取系统的gsm.sim.operator.numeric 属性
String where = "numeric=\"" + android.os.SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC, "")+ "\""; // 调用系统提供的ContentProvider查询数据库
Cursor cursor = getContentResolver().query(Telephony.Carriers.CONTENT_URI, new String[] {
"_id", "name", "apn", "type"}, where, null,
Telephony.Carriers.DEFAULT_SORT_ORDER);
// 找到当前Activity中的PreferenceGroup
PreferenceGroup apnList = (PreferenceGroup) findPreference("apn_list");
apnList.removeAll(); ArrayList<Preference> mmsApnList = new ArrayList<Preference>(); mSelectedKey = getSelectedApnKey();
cursor.moveToFirst();
// 迭代查询数据库
while (!cursor.isAfterLast()) {
String name = cursor.getString(NAME_INDEX);
String apn = cursor.getString(APN_INDEX);
String key = cursor.getString(ID_INDEX);
String type = cursor.getString(TYPES_INDEX);
// 新建一个 ApnPreference,填充里面控件的值
ApnPreference pref = new ApnPreference(this); pref.setKey(key);
pref.setTitle(name);
pref.setSummary(apn);
pref.setPersistent(false);
pref.setOnPreferenceChangeListener(this); boolean selectable = ((type == null) || !type.equals("mms"));
pref.setSelectable(selectable);
if (selectable) {
if ((mSelectedKey != null) && mSelectedKey.equals(key)) {
pref.setChecked();
}
apnList.addPreference(pref);
} else {
mmsApnList.add(pref);
}
cursor.moveToNext();
}
cursor.close(); for (Preference preference : mmsApnList) { // 将这个preference增加到apnList中
apnList.addPreference(preference);
}
}

这里的ApnPreference是我们自定义的一个类。继承于Preference。

这个类非常easy 就是依据R.layout.apn_preference_layout文件来对APN配置页面的每一项进行布局的。主要是两个TextView和一个RadioButton。

这里我们已经知道了进入APN设置后,系统是怎样将数据库中已经存在的APN条目读取出来并通过UI的形式显示出来的。

那么我们又怎么加入自定义的APN配置信息呢?这就要用到Options Menu了,在手机上当我们按下Menu键的时候弹出一个列表。单击这个列表每一项我们能够进入对应的Activity等。

    @Override
public boolean onCreateOptionsMenu(Menu menu) { // 当按下Menu键的时候调用
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_NEW, 0, // 添加两个条目,分别用于添加APN和恢复出厂设置
getResources().getString(R.string.menu_new))
.setIcon(android.R.drawable.ic_menu_add);
menu.add(0, MENU_RESTORE, 0,
getResources().getString(R.string.menu_restore))
.setIcon(android.R.drawable.ic_menu_upload);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { // 响应Menu按下的列表
case MENU_NEW: // 添加APN
addNewApn();
return true; case MENU_RESTORE: // 恢复出厂设置
restoreDefaultApn();
return true;
}
return super.onOptionsItemSelected(item);
} private void addNewApn() { // 启动新的Activity,动作为Intent.ACTION_INSERT
startActivity(new Intent(Intent.ACTION_INSERT, Telephony.Carriers.CONTENT_URI));
} // 响应 设置页面每一行条目的单击事件
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
int pos = Integer.parseInt(preference.getKey());
Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos);
// 对当前选中页面进行编辑,也是启动一个Activity
startActivity(new Intent(Intent.ACTION_EDIT, url));
return true;
} public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.d(TAG, "onPreferenceChange(): Preference - " + preference
+ ", newValue - " + newValue + ", newValue type - "
+ newValue.getClass());
if (newValue instanceof String) {
setSelectedApnKey((String) newValue);
}
return true;
}

不管是添加APN还是对原有的APN条目进行编辑,我们都是通过进入一个新的Activity来完毕的。

以下我们找到匹配Intent.ACTION_EDIT。Intent.ACTION_INSERT

我们找到相应的Activity ApnEditor。ApnEditor也是一个继承与PreferenceActivity的类。同一时候实现了SharedPreferences.onSharedPreferenceChangeListener和Preference.OnPreferenceChangeListener接口。

    @Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle); addPreferencesFromResource(R.xml.apn_editor); sNotSet = getResources().getString(R.string.apn_not_set);
mName = (EditTextPreference) findPreference("apn_name");
mApn = (EditTextPreference) findPreference("apn_apn");
mProxy = (EditTextPreference) findPreference("apn_http_proxy");
mPort = (EditTextPreference) findPreference("apn_http_port");
mUser = (EditTextPreference) findPreference("apn_user");
mServer = (EditTextPreference) findPreference("apn_server");
mPassword = (EditTextPreference) findPreference("apn_password");
mMmsProxy = (EditTextPreference) findPreference("apn_mms_proxy");
mMmsPort = (EditTextPreference) findPreference("apn_mms_port");
mMmsc = (EditTextPreference) findPreference("apn_mmsc");
mMcc = (EditTextPreference) findPreference("apn_mcc");
mMnc = (EditTextPreference) findPreference("apn_mnc");
mApnType = (EditTextPreference) findPreference("apn_type"); mAuthType = (ListPreference) findPreference(KEY_AUTH_TYPE);
mAuthType.setOnPreferenceChangeListener(this); mProtocol = (ListPreference) findPreference(KEY_PROTOCOL);
mProtocol.setOnPreferenceChangeListener(this); mRoamingProtocol = (ListPreference) findPreference(KEY_ROAMING_PROTOCOL);
// Only enable this on CDMA phones for now, since it may cause problems on other phone
// types. (This screen is not normally accessible on CDMA phones, but is useful for
// testing.)
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
if (tm.getCurrentPhoneType() == Phone.PHONE_TYPE_CDMA) {
mRoamingProtocol.setOnPreferenceChangeListener(this);
} else {
getPreferenceScreen().removePreference(mRoamingProtocol);
} mCarrierEnabled = (CheckBoxPreference) findPreference(KEY_CARRIER_ENABLED); mBearer = (ListPreference) findPreference(KEY_BEARER);
mBearer.setOnPreferenceChangeListener(this); mRes = getResources(); final Intent intent = getIntent();
final String action = intent.getAction(); mFirstTime = icicle == null; if (action.equals(Intent.ACTION_EDIT)) {
mUri = intent.getData();
Log.w(TAG, "llping Edit action:"+mUri.toString());
} else if (action.equals(Intent.ACTION_INSERT)) {
if (mFirstTime || icicle.getInt(SAVED_POS) == 0) {
mUri = getContentResolver().insert(intent.getData(), new ContentValues());
} else {
mUri = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI,
icicle.getInt(SAVED_POS));
}
Log.w(TAG, "llping Insert action:"+mUri.toString());
mNewApn = true;
// If we were unable to create a new note, then just finish
// this activity. A RESULT_CANCELED will be sent back to the
// original activity if they requested a result.
if (mUri == null) {
Log.w(TAG, "Failed to insert new telephony provider into "
+ getIntent().getData());
finish();
return;
} // The new entry was created, so assume all will end well and
// set the result to be returned.
setResult(RESULT_OK, (new Intent()).setAction(mUri.toString())); } else {
finish();
return;
} mCursor = managedQuery(mUri, sProjection, null, null);
mCursor.moveToFirst(); fillUi();
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

Android系统APN配置具体解释的更多相关文章

  1. Android系统定制——Download Android System 及加载system镜像文件

    定制android系统(配置及相关系统的镜像文件),具体可参考:Driver_All_in_One_V1.0——MT6735_6753.pdf文档,特别需要理解的是Download部分. 与之对应的软 ...

  2. Android系统固件定制方式

    target_product.mkAndroid系统在构建关于某种产品的固件时,一般会根据特定于该产品的具体target_product.mk来配置生成整个Android系统./target_prod ...

  3. Android Google Map v2具体解释:开发环境配置

    Android Google Map v2具体解释:开发环境配置                                       --转载请注明出处:coder-pig 说在前面: 说到地 ...

  4. 在Mac系统上配置Android真机调试环境

    在Mac系统上配置Android真机调试环境 mac上配置安卓环境还说挺方便的,真机调试也比win上要好一些.win上被各种软件强行安装了xxx助手. 在mac上就了一个干净的感觉. 下载Androi ...

  5. Android中实现全屏、无标题栏的两种办法(另附Android系统自带样式的解释)

    在进行UI设计时,我们经常需要将屏幕设置成无标题栏或者全屏.要实现起来也非常简单,主要有两种方法:配置xml文件和编写代码设置. 1.在xml文件中进行配置 在项目的清单文件AndroidManife ...

  6. Mac系统cocos2dx + android 开发环境配置

    Mac系统cocos2dx + android 开发环境配置 /****************************************************** 这遍文章主要转载自:htt ...

  7. Android APN配置

    APN概念 APN(Access Point Name),即“接入点名称”,用来标识GPRS的业务种类,目前分为两大类:CMWAP(通过GPRS访问WAP业务).CMNET(除了WAP以外的服务目前都 ...

  8. crt转cer ,6.0以上的android系统证书请求配置

    1.在服务器人员,给你发送的crt证书后,进到证书路径,执行下面语句  openssl x509 -in 你的证书.crt -out 你的证书.cer -outform der 这样你就可以得到cer ...

  9. Linux 桌面系统字体配置要略

    字体显示效果测试 这一段是为了测试宋体字的显示效果,包括宋体里面自带的英文字体,“This is english,how does it look like?”.这一行是小字.后面几个字是加粗的宋体. ...

随机推荐

  1. leetcode 之 Permutation Sequence

    Permutation Sequence The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

  2. spring 定义自己的标签 学习

    自己的自定义配置文件spring 在,有时你想要做一些配置信息的数据结构.基于扩展生意做. 首先: 在项目META-INF文件夹中创建两个文件spring.handlers,和spring.shcem ...

  3. SVN 的revert操作

  4. Fedora16 安装相关

    安装BCM4312无线网卡驱动 Linux系统BCM4312无线网卡驱动的安装 联想Y450 Linux系统 无线网卡驱动安装 准备工作: Broadcom官网驱动下载地址 http://www.br ...

  5. Windows phone 8 学习笔记(1) 触控输入

    原文:Windows phone 8 学习笔记(1) 触控输入 Windows phone 8 的应用 与一般的Pc应用在输入方式上最大的不同就是:Windows phone 8主要依靠触控操作.因此 ...

  6. Linux cp -a用法

    对于cp -a最主要的用法是在保留原文件属性的前提下复制文件.其实还有个很好的用法,如下: 大家知道linux下复制目录可以通过,cp -r dirname destdir 但是这样复制的目录属性会发 ...

  7. 使用JavaMail发送和接收电子邮件

    一. 为什么要学习JavaMail 为什么要学习JavaMail开发? 如今非常多WEB应用在开发时都须要集成邮件发送功能.比如: 1. 给新注冊的用户自己主动发送一封包括其注冊信息的欢迎E-Mail ...

  8. 树形dp专辑

    hdu 2196 http://acm.hdu.edu.cn/showproblem.php?pid=2196 input 5//5个结点 1 1//表示结点2到结点1有一条权值为1的边 2 1//表 ...

  9. 初识google多语言通信框架gRPC系列(二)编译gRPC

    目录 一.概述 二.编译gRPC 三.C#中使用gRPC 四.C++中使用gRPC 无论通过哪种语言调用gRPC,都必须要编译gRPC,因为生成proto访问类时,除了产生标准的数据定义类之外,还需要 ...

  10. 将 Android* x86 NDK 供 Eclipse* 而移植 NDK 演示示例应用程序

    目标 面向 Eclipse (ADT) 的 Android 插件如今支持基于 NDK 的应用开发. 其可自己主动生成项目和构件文件以及代码存根.并可集成到整个 Android 应用开发中(构建原生库. ...