Detailed explanation of Android system APN Configuration

Source: Internet
Author: User

The APN configuration of the Android system has been explained in this document in the settings.apk configuration of the debugging system. You have manually added the APN configuration option in the settings, but you still cannot see it on the interface. So I followed the code and thought it was a simple page display problem. It doesn't matter, so I quickly caught up with the HAL layer (NND ). first, let's take a look at the source code of settings.apk, which is located in the packages/apps/Settings/src/com/android/settings/directory. First, find the ApnSettings class, inherit from PreferenceActivity, and implement Preference. onPreferenceChangeListener interface. PreferencesActivity is an Activity used by Android to implement the program setting interface and Parameter Storage. [Java] public class ApnSettings extends PreferenceActivity implements Preference. onPreferenceChangeListener {// restore the URI public static final String RESTORE_CARRIERS_URI = "content: // telephony/carriers/restore"; // normal URI, used for ContentPrivoder to save the 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); // two handles, used to restore the factory settings private RestoreApnUiHandler handle; private Intent handle; private String mSelectedKey; // Intent filter received by multicast private IntentFilter mMobileStateFilter; private final BroadcastReceiver mMobileStateReceiver = new BroadcastReceiver () {@ Override public v Oid 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); // configure the view based on the xml file when the activity is created. // In fact, the res/xml/apn_settings.xml file is an empty PreferenceScreen addPreferencesFromResource (R. xml. apn_settings); getListView (). setItemsCanFocus (true); // if there is a List, the focus is obtained. // This creates an Inter filter and the filtering action is ACTIO. N_ANY_DATA_CONNECTION_STATE_CHANGED mMobileStateFilter = new IntentFilter (TelephonyIntents. ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);} @ Override protected void onResume () {super. onResume (); // register a broadcast receiver registerReceiver (mMobileStateReceiver, mMobileStateFilter); if (! MRestoreDefaultApnMode) {// if the factory settings are not restored, fillList (); // fill the ListView of the Activity} else {showDialog (DIALOG_RESTORE_DEFAULTAPN); }}1) first in onCreate () in the method, configuring the interface view based on the apn_settings.xml file is actually a PreferenceScreen. Create an Intent filter with the filtering action ACTION_ANY_DATA_CONNECTION_STATE_CHANGED. 2) register a broadcast receiver in the onResume () method. When the above ACTION_ANY_DATA_CONNECTION_STATE_CHANGED action is received, call the onReceive () method of mMobileStateReceiver. The purpose is to display the configuration information of the APN when we enter the APN settings. Then determine whether to restore the factory settings. If not, call the fillList () method to fill in the current Activity and display the configuration information of the APN. 3) first obtain the system property gsm. sim. operator. numeric: queries the database (in/data/com. android. providers. telephony under telephony. the carriers table in the db database) to obtain the corresponding configuration information. Then, fill it in each ApnPreference, and display each ApnPreference to the current PreferenceGroup. [Java] private void fillList () {// obtain the system's gsm. sim. operator. numeric attribute String where = "numeric = \" "+ android. OS. systemProperties. get (TelephonyProperties. PROPERTY_ICC_OPERATOR_NUMERIC, "") + "\" "; // call the ContentProvider provided by the system to query the database Cursor cursor = getContentResolver (). query (Telephony. carriers. CONTENT_URI, new String [] {"_ id", "name", "apn", "type"}, where, null, Telephony. carriers. DEFAULT_SORT_ORDER );// Locate the PreferenceGroup apnList = (PreferenceGroup) findPreference ("apn_list"); apnList in the current Activity. removeAll (); ArrayList <Preference> mmsApnList = new ArrayList <Preference> (); mSelectedKey = getSelectedApnKey (); cursor. moveToFirst (); // iteratively queries the database 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); // create an ApnPreference and fill in the control value 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) {// Add this preference to apnList. addPreference (preference) ;}} here, ApnPreference is a class defined by us and inherits from Preference. This class is very simple, that is, layout each item on the APN configuration page according to the R. layout. apn_preference_layout file. There are two textviews and one RadioButton. Now we know how the system reads existing APN entries in the database and displays them through the UI after you enter the APN settings. So how can we add our own defined APN configuration information? This requires the Options Menu. When we press the Menu key on the mobile phone, a list is displayed. Click each item in the list to enter the corresponding Activity. [Java] @ Override public boolean onCreateOptionsMenu (Menu menu) {// call super when the Menu key is pressed. onCreateOptionsMenu (menu); menu. add (0, MENU_NEW, 0, // add two entries, respectively used to add APN and restore factory settings 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;} @ Overrid E public boolean onOptionsItemSelected (MenuItem item) {switch (item. getItemId () {// response to the list pressed by Menu case MENU_NEW: // Add APN addNewApn (); return true; case MENU_RESTORE: // restore factory settings restoreDefaultApn (); return true;} return super. onOptionsItemSelected (item);} private void addNewApn () {// start a new Activity. The action is Intent. ACTION_INSERT startActivity (new Intent (Intent. ACTION_INSERT, Telephony. carriers. CONTENT_U RI);} // click the event @ Override public boolean onPreferenceTreeClick (PreferenceScreen preferenceScreen, Preference preference) for each row in the response settings page {int pos = Integer. parseInt (preference. getKey (); Uri url = ContentUris. withAppendedId (Telephony. carriers. CONTENT_URI, pos); edit the selected page, and start an Activity startActivity (new Intent (Intent. ACTION_EDIT, url); return true;} public boolean onPreferenceChange (Preference p Reference, Object newValue) {Log. d (TAG, "onPreferenceChange (): Preference-" + preference + ", newValue-" + newValue + ", newValue type-" + newValue. getClass (); if (newValue instanceof String) {setSelectedApnKey (String) newValue);} return true;} whether to add or edit an APN entry, we are all done by entering a new Activity. Next we find the Activity ApnEditor that matches Intent. ACTION_INSERT and Intent. ACTION_INSERT. ApnEditor is also a class that inherits the PreferenceActivity and implements the SharedPreferences. identifier and Preference. OnPreferenceChangeListener interfaces. [Java] @ Override protected void onCreate (Bundle icicle) {super. onCreate (icicle); addPreferencesFromResource (R. xml. apn_editor); sNotSet = getResources (). getString (R. string. callback); 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 ("Priority "); 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. terminate (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);} og. 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 ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.