Next, now analyze Android\vendor\qcom\opensource\fm\fmapp2\src\com\caf\fmradio\stationlistactivity.java
Protectedvoid onCreate (Bundle savedinstancestate) method
Binding Fmradioservice Services
Bindservice ((Newintent ()). SetClass (This, fmradioservice.class), OSC, 0);
Instantiating a ListView object
Mstationlist= (ListView) Findviewbyid (r.id.station_list);
Context Menu Listener Events
Mstationlist.setoncreatecontextmenulistener (this);
ListView listens on each item event
Mstationlist.setonitemclicklistener (Newonitemclicklistener ()
Call Mservice.tune ((int) ((ffreq * 1000)); Method FM
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.station_list); Bindservice ((New Intent ()). SetClass (This, fmradioservice.class), OSC, 0); Mstationlist = (ListView) Findviewbyid (r.id.station_list); Mpresetlist = new Presetlist ("Stationlist"); Mstationlist.setoncreatecontextmenulistener (this); Mstationlist.setonitemclicklistener (New Onitemclicklistener () {@Override public void Onitemclick (Ad Apterview<?> arg0, View arg1, int arg2, long arg3) {String freq = ((hashmap<st Ring, string>) Madapter.getitem (arg2)). Get ("Freq"); Float ffreq = float.parsefloat (freq); if (mservice! = null) {try {mservice.tune ((int) ((ffreq * 1000))); Finish ();} catch (RemoteException e) {e.printstacktrace (); }} else {log.d (LogTag, "Mservice is null ..."); } } }); }</span>
Context Menu
public void Oncreatecontextmenu (Contextmenumenu, View v,contextmenuinfo menuinfo)
Add from naming and deleting features
Menu.add (0,context_menu_rename, 0, getString (r.string.preset_rename));
Menu.add (0,context_menu_delete, 0, getString (r.string.preset_delete));
Title
Menu.setheadertitle (getString (r.string.station_name) +getnamefromid (Mitemid));
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >public void Oncreatecontextmenu (ContextMenu menu, View V, contextmenuinfo menuinfo) { //TODO auto-generated method Stub adaptercontextmenuinfo mi = (adaptercontextmenuinfo) menuinfo; Menu.add (0, context_menu_rename, 0, getString (r.string.preset_rename)); Menu.add (0, Context_menu_delete, 0, getString (r.string.preset_delete)); Mitemid = mi.position; Menu.setheadertitle (getString (r.string.station_name) +getnamefromid (Mitemid)); } </span>
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >protected void onpreparedialog (int id, Dialog Dialog, Bundle b) {//TODO auto-generated method Stub// Super.onpreparedialog (ID, Dialog); After the change system language, the current function would be executed before//Onresume, so execute the load to ensure a Dapter is not null. Load (); Switch (ID) {Case DIALOG_RENAME_ID:mRenameDialog.setTitle (getString (r.string.station_name) +getnamefrom Id (Mitemid)); Final EditText EditText = (EditText) mrenamedialog. Findviewbyid (R.id.name); Edittext.settext (Getnamefromid (Mitemid)); Button bOk = (button) Mrenamedialog.findviewbyid (R.id.save); Bok.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { String rename = Edittext.gettext (). toString (); if (textutils.isempty (rename) | | Textutils.isEmpty (Rename.trim ())) {Context context = Getapplicationcontext (); Toast Toast = Toast.maketext (context, getString (r.string.station_name_empty), toast.length_ short); Toast.setgravity (Gravity.center_horizontal | gravity.center_vertical, 0, 0); Toast.show (); } else if (stationnameexist (rename)) {Context context = Getapplicationcontext (); Toast Toast = Toast.maketext (context, getString (r.string.station_name_exist, rename) , Toast.length_short); Toast.setgravity (Gravity.center_horizontal | gravity.center_vertical, 0, 0); Toast.show (); } else {savestationname (mitemid,rename); Mrenamedialog.dismiss (); } } }); Button bcancel = (Button) Mrenamedialog.findviewbyid (r.id.cancel); Bcancel.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View V ) {//TODO auto-generated Method Stub Mrenamedialog.dismiss (); } }); Break Case DIALOG_DELETE_ID:mDeleteDialog.setTitle (getString (R.string.station_list_delete_station, Getnamefromid (mIt (emId))); TextView prompt = (TextView) Mdeletedialog.findviewbyid (r.id.prompt); Prompt.settext (GetString (R.string.station_list_delete_station_prompt,getnamefromid (Mitemid))); button bdelete = (button) Mdeletedialog.findviewbyid (R.id.delete); Bdelete.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { Deletestation (Mitemid); Mdeletedialog.dismiss (); } }); Button bCanceldelete = (Button) Mdeletedialog.findviewbyid (r.id.cancel); Bcanceldelete.setonclicklistener (New View.onclicklistener () {@Override public void OnClick ( View v) {Mdeletedialog.dismiss (); } }); Break }}</span>
Determine if a station name exists
Privateboolean stationnameexist (String name)
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >private boolean stationnameexist (String name) { for (hashmap<string, string> item:list) { if (item! = Null && name.equals (item.get ("name")) { return true; } } return false; } </span>
Rename Save method
Savestationname (Mitemid,rename);
Mrenamedialog.dismiss ();
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >private void savestationname (int id, String name) { integer stationindex = mindex.get (new integer (ID)); Sharedpreferences sp = getsharedpreferences ( fmradio.scan_station_prefs_name, 0); Sharedpreferences.editor Editor = Sp.edit (); Editor. Putstring (Fmradio.station_name + (Stationindex.intvalue ()), NAME); Editor.commit (); Load (); } </span>
Delete a radio method
Deletestation (Mitemid);
Mdeletedialog.dismiss ();
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > private void deletestation (int id) { sharedpreferences sp = getsharedpreferences (fmradio.scan_station_prefs_ NAME, 0); Integer Stationindex = mindex.get (new integer (ID)); Sharedpreferences.editor Editor = Sp.edit (); Editor.remove (Fmradio.station_name + (Stationindex)); Editor.remove (fmradio.station_frequency + (Stationindex)); Editor.commit (); Load (); } </span>
Load load () after saving the name by calling Sharedpreferences;
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >protected void Load () {list.clear (); Mindex.clear (); Sharedpreferences sp = getsharedpreferences (fmradio.scan_station_prefs_name, 0); int station_number = sp.getint (fmradio.num_of_stations, 0); for (int i = 1; I <= station_number; i++) {hashmap<string, string> item = new hashmap<string, Stri Ng> (); String name = sp.getstring (Fmradio.station_name + i, ""); int frequency = Sp.getint (fmradio.station_frequency + i, 0); if (!name.equals (") && frequency! = 0) {item.put (" name ", name); Item.put ("Freq", String.valueof (frequency/1000.0f)); Mindex.put (New Integer (List.size ()), new Integer (i)); List.add (item); }} madapter = new Simpleadapter (this, list, R.layout.station_list_item, new string[] {"Name "," Freq "}, new int[] {r.id.name, R.id.freq}); Mstationlist.setadapter (Madapter); }</span>
Method
Privatevoid savestationname (int id, String name)
Editor.putstring (fmradio.station_name+ (Stationindex.intvalue ()),
Privatevoid deletestation (int id) Delete method, delete name and frequency
Editor.remove (Fmradio.station_name + (stationindex));
Editor.remove (fmradio.station_frequency + (stationindex));
Get the frequency method by ID
Privateint getfrequencyfromid (int id)
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >private int getfrequencyfromid (int id) { String freq = ((hashmap<string, string>) Madapter.getitem (ID)) C1/>.get ("Freq"); Float ffreq = float.parsefloat (freq); return (int) ((ffreq *)); } </span>
Get the frequency name method with ID
privatestring getnamefromid (int id)
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > private string getnamefromid (int id) { string name = ((hashmap<string, string>) Madapter.getitem (ID)) . Get ("name"); return name; </span>
Load method, clear ListView List Contents and index ID, get the number of channels from sharedpreferences
Use the For loop to add XML data (station name and frequency) to the list list using Simpleadapter to display the data in the UI interface
protected void Load () {
Item.put ("name", name);
Item.put ("Freq", String.valueof (frequency/1000.0f));
Mindex.put (Newinteger (List.size ()), New Integer (i));
List.add (item);
}
madapter= New Simpleadapter (this, list, R.layout.station_list_item,
New string[] {"Name", "Freq"}, new int[] {r.id.name,
R.id.freq});
The OnDestroy () method of the Stationlistactivity class is used to unbind the Fmradioservice service.
Unbindservice (OSC);
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >protected void OnDestroy () { unbindservice (OSC); Mservice = null; Super.ondestroy (); } </span>
Android FM Module Learning four source analysis (vii)