Specific explanations for the APN configuration of the Android system

Source: Internet
Author: User

Specific explanations for the APN configuration of the Android system

These days have been tuning the system native settings.apk inside the APN configuration issue. The APN configuration option is manually added to the settings. But I still can't see it on the interface. So followed by the code. Originally thought is a simple page display problem. This one doesn't matter. We're going to go after the HAL layer (NND).

First look at the source code of SETTINGS.APK, located under the packages/apps/settings/src/com/android/settings/folder: First find the Apnsettings class. Inherits from Preferenceactivity, and implements the Preference.onpreferencechangelistener interface. Preferencesactivity is an activity that is specifically used in Android to implement the program settings interface and the parameter storage. We will not repeat it here.

public class Apnsettings extends Preferenceactivity implements Preference.onpreferencechangelistener {//factory reset URI public static final String Restore_carriers_uri = "Content://telephony/carriers/restore";//Normal URI. Used for Contentprivoder to save APN configuration information public static final String Preferred_apn_uri = "CONTENT://TELEPHONY/CARRIERS/PREFERAPN"; Private static final URI Defaultapn_uri = Uri.parse (Restore_carriers_uri);p rivate static final uri Preferapn_uri = Uri.par SE (Preferred_apn_uri);//two handles. Used to restore factory settings private Restoreapnuihandler mrestoreapnuihandler;private Restoreapnprocesshandler Mrestoreapnprocesshandler;private String mselectedkey;//Multicast received intent filter private Intentfilter mmobilestatefilter; Private final Broadcastreceiver mmobilestatereceiver = new Broadcastreceiver () {@Overridepublic 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; }}}; @Overrideprotected void OnCreate (Bundle icicle) {super.oncreate (icicle);//The view is configured according to an XML file when activity is created. In fact res/xml/apn_settings.xml This file is an empty preferencescreenaddpreferencesfromresource (r.xml.apn_settings); Getlistview (). Setitemscanfocus (true);//Assume that a list gets the focus//This creates a inter filter. The filter action is Action_any_data_connection_state_changedmmobilestatefilter = new Intentfilter (telephonyintents.acti on_any_data_connection_state_changed);} @Overrideprotected void Onresume () {super.onresume ();//Register a broadcast recipient Registerreceiver (Mmobilestatereceiver, Mmobilestatefilter); if (!mrestoredefaultapnmode) {//Assume not to restore factory settings filllist ();//Populate Activity's ListView} else {ShowDialog ( DIALOG_RESTORE_DEFAULTAPN);}}}

1) Here first in the OnCreate () method, the view of the interface is configured according to the Apn_settings.xml file. is actually a preferencescreen. Create a intent filter that filters the action to action_any_data_connection_state_changed.

2) then in the Onresume () method, register a broadcast recipient. When you receive the above action_any_data_connection_state_changed action. Call

Mmobilestatereceiver's OnReceive () method. The purpose is to, when we enter the APN settings, this is to plug in the SIM card to display the APN configuration information. Then infer whether the factory settings need to be restored, assuming not. The Filllist () method is called to populate the current activity, showing the APN configuration information.

3) First obtain the system attribute Gsm.sim.operator.numeric, based on this parameter through the system-provided ContentProvider query database (located in/data/data/ Carriers table in the Telephony.db database under Com.android.providers.Telephony). Get the appropriate configuration information. It is then populated into each apnpreference, and finally each apnpreference is displayed on the current preferencegroup.

private void Filllist () {//Gets the Gsm.sim.operator.numeric property of the system String where = "numeric=\" "+ Android.os.SystemPropert  Ies.get (Telephonyproperties.property_icc_operator_numeric, "") + "\" ";//Invoke system-supplied ContentProvider query database cursor cursor = Getcontentresolver (). Query (Telephony.Carriers.CONTENT_URI, new string[] {"_id", "name", "APN", "type"}, W Here, NULL, Telephony.Carriers.DEFAULT_SORT_ORDER);//Find Preferencegroup in the current activity Preferencegroup        Apnlist = (preferencegroup) findpreference ("Apn_list");        Apnlist.removeall ();        arraylist<preference> mmsapnlist = new arraylist<preference> ();        Mselectedkey = Getselectedapnkey (); Cursor.movetofirst ();//Iterate Query database while (!cursor.isafterlast ()) {String name = cursor.getstring (Name_inde            X);            String APN = cursor.getstring (Apn_index);            String key = Cursor.getstring (Id_index); String type = cursor.getstring (types_index);//Create a newPnpreference, fills the value of the inside control 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 apnlist.addpreference (Preference); }}

The apnpreference here is a class that we have customized. Inherit from preference.

This class is very easy to lay out each item of the APN configuration page based on the R.layout.apn_preference_layout file. It is mainly two textview and one radiobutton.

Here we know how the system will read the APN entries that already exist in the database and display them in the form of a UI after entering the APN settings.

So how do we add the custom APN configuration information? This is going to use the Options menu, on the phone when we press the menu button to pop up a list. Click this list for each item we can access the corresponding activity and so on.

    @Override public boolean Oncreateoptionsmenu (Menu menu) {//Super.oncreateoptionsmenu (menu) is called when the menu key is pressed;                Menu.add (0, Menu_new, 0,//add two entries, respectively, 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 (Andro Id.        R.drawable.ic_menu_upload);    return true;        } @Override public boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {///Response menu pressed list            Case menu_new://Add APN addnewapn ();        return true;            Case menu_restore://Restore factory settings restoredefaultapn ();        return true;    } return super.onoptionsitemselected (item); The private void Addnewapn () {//Starts a new activity with the action Intent.action_insert startactivity (new Intent (Intent.action_inser    T, Telephony.Carriers.CONTENT_URI)); }    //Response Settings page Click events for each line entry @Override public boolean Onpreferencetreeclick (Preferencescreen preferencescreen, Preference Prefe        rence) {int pos = Integer.parseint (Preference.getkey ()); Uri url = contenturis.withappendedid (Telephony.Carriers.CONTENT_URI, POS);//Edit the currently selected page and start an activity Startactivi        Ty (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.g        Etclass ());        if (newvalue instanceof string) {Setselectedapnkey (string) newvalue);    } return true; }

Whether you are adding an APN or editing an existing APN entry, we are finished by entering a new activity.

Below we find matching intent.action_edit. Intent.action_insert
We found the corresponding activity apneditor. Apneditor is also a class of inheritance and Preferenceactivity. The Sharedpreferences.onsharedpreferencechangelistener and Preference.onpreferencechangelistener interfaces are implemented at the same time.

    @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 the this on the CDMA phones for now, since it may cause problems on the other phone//types.        (This is a normally accessible on CDMA phones, and 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.ge            TInt (Saved_pos));            }LOG.W (TAG, "llping Insert Action:" +muri.tostring ());            MNEWAPN = true;  If we were unable to create a new note, then the just finish//this activity.      A result_canceled'll is sent back to the      Original activity if they requested a result. if (MUri = = null) {LOG.W (TAG, "Failed to insert new telephony provider into" + Get                Intent (). GetData ());                Finish ();            Return            }//The new entry is created, so assume all would end well and//set the result of be returned.        Setresult (RESULT_OK, (New Intent ()). Setaction (Muri.tostring ()));            } else {finish ();        Return        } mcursor = Managedquery (MUri, sprojection, NULL, NULL);        Mcursor.movetofirst ();    Fillui (); }


Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Specific explanations for the APN configuration of the Android system

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.