android 修改默认APN
16lz
2021-01-23
Settings > More > Mobile networks > Access Point Names
在此选项中, 我手中的平台,目前是没有任何选项显示的.
代码:
packages/apps/Settings/src/com/android/settings/ApnSettings.java//以下代码用于构建一个列表, 从数据库中读取数据并填充到表中.//注意下面的代码有一些过滤条件private void fillList() { String where = "numeric=\"" + android.os.SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC, "") + "\""; String operatorDongle = SystemProperties.get("gsm.dongle.operator");Log.d(TAG,"fillList:operatorDongle:"+operatorDongle);if(operatorDongle != null) {if(!operatorDongle.equals("llll") && false)where = "numeric=\""+operatorDongle+"\"";}//从数据库获取, 下面代码会说明数据库是如何创建及始化的. Cursor cursor = getContentResolver().query(Telephony.Carriers.CONTENT_URI, new String[] { "_id", "name", "apn", "type"}, null, null, Telephony.Carriers.DEFAULT_SORT_ORDER); PreferenceGroup apnList = (PreferenceGroup) findPreference("apn_list"); apnList.removeAll(); ArrayList mmsApnList = new ArrayList(); 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 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) { apnList.addPreference(preference); } }
数据库创建:
packages/providers/TelephonyProvider/src/com/android/providers/telephony/TelephonyProvider.javaprivate static class DatabaseHelper extends SQLiteOpenHelper { // Context to access resources with private Context mContext; /** * DatabaseHelper helper class for loading apns into a database. * * @param context of the user. */ public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, getVersion(context)); mContext = context; } private static int getVersion(Context context) { // Get the database version, combining a static schema version and the XML version Resources r = context.getResources(); XmlResourceParser parser = r.getXml(com.android.internal.R.xml.apns); try { XmlUtils.beginDocument(parser, "apns"); int publicversion = Integer.parseInt(parser.getAttributeValue(null, "version")); return DATABASE_VERSION | publicversion; } catch (Exception e) { Log.e(TAG, "Can't get version of APN database", e); return DATABASE_VERSION; } finally { parser.close(); } } @Override public void onCreate(SQLiteDatabase db) { // Set up the database schema 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," + "authtype INTEGER," + "type TEXT," + "current INTEGER," + "protocol TEXT," + "roaming_protocol TEXT," + "carrier_enabled BOOLEAN," + "bearer INTEGER);"); initDatabase(db);//创建后,初始数据 } private void initDatabase(SQLiteDatabase db) { // Read internal APNS data Resources r = mContext.getResources(); XmlResourceParser parser = r.getXml(com.android.internal.R.xml.apns);//从frameworks/base/core/res/中获取XML并添加到数据库中. int publicversion = -1; try { XmlUtils.beginDocument(parser, "apns"); publicversion = Integer.parseInt(parser.getAttributeValue(null, "version")); loadApns(db, parser); } catch (Exception e) { Log.e(TAG, "Got exception while loading APN database.", e); } finally { parser.close(); } // Read external APNS data (partner-provided) XmlPullParser confparser = null; // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system". File confFile = new File(Environment.getRootDirectory(), PARTNER_APNS_PATH);//从/etc/apns-conf.xml中获取并添加到数据库中. FileReader confreader = null; try { confreader = new FileReader(confFile); confparser = Xml.newPullParser(); confparser.setInput(confreader); XmlUtils.beginDocument(confparser, "apns"); // Sanity check. Force internal version and confidential versions to agree int confversion = Integer.parseInt(confparser.getAttributeValue(null, "version")); if (publicversion != confversion) { throw new IllegalStateException("Internal APNS file version doesn't match " + confFile.getAbsolutePath()); } loadApns(db, confparser); } catch (FileNotFoundException e) { // It's ok if the file isn't found. It means there isn't a confidential file // Log.e(TAG, "File not found: '" + confFile.getAbsolutePath() + "'"); } catch (Exception e) { Log.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e); } finally { try { if (confreader != null) confreader.close(); } catch (IOException e) { } } }
所以, 一般默认的APN有两个地方可添加:
1. frameworks/base/core/res/res/xml/apns.xml
2. RK平台中,采用此路径:
device/rockchip/common/phone/etc/apns-full-conf.xml
XML的内容如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> to:
更多相关文章
- Android 的http通信(原生代码)
- [Android] 一份代码,两个版本
- Android 中解析JSON形式的数据
- Android应用程序组件Content Provider的共享数据更新通知机制分
- Android一键锁屏代码
- Android SQLite数据库升级的问题
- 使用SharedPreferences存储和读取数据
- Android:Content Provider数据共享