Android FM module learning-4 source code analysis (7)

Source: Internet
Author: User

Android FM module learning-4 source code analysis (7)

Now we analyze androidendorqcomopensourcemmapp2srccomcafmradioStationListActivity. java

 

In the protectedvoid onCreate (Bundle savedInstanceState) method

Bind FMRadioService

BindService (newIntent (). setClass (this, FMRadioService. class), osc, 0 );

Instantiate a ListView object

MStationList = (ListView) findViewById (R. id. station_list );

Context Menu listening event

MStationList. setOnCreateContextMenuListener (this );

ListView listens to every Item event

MStationList. setOnItemClickListener (newOnItemClickListener ()

Call mService. tune (int) (fFreq * 1000); Method Frequency Modulation

 

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(AdapterView
  arg0, View arg1, int arg2,                    long arg3) {                String freq = ((HashMap
 
  ) 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........);                }            }        });    }
 

 

Context Menu

Public void onCreateContextMenu (ContextMenumenu, View v, ContextMenuInfo menuInfo)

Add the slave naming and deletion Functions

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 ));

 

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));    }

 

 

 

protected void onPrepareDialog(int id, Dialog dialog, Bundle b) {        // TODO Auto-generated method stub        // super.onPrepareDialog(id, dialog);        // After change system language, current function will be executed before        // onResume , so execute load to ensure adapter is not null.        load();        switch (id) {        case DIALOG_RENAME_ID:            mRenameDialog.setTitle(getString(R.string.station_name)+getNameFromId(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(mItemId)));            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;        }    }


 

Determine whether a radio station name exists

Privateboolean stationNameExist (String name)

 

private boolean stationNameExist(String name) {        for (HashMap
 
   item : list) {            if (item != null && name.equals(item.get(name))) {                return true;            }        }        return false;    }
 

 

Rename and save Method

SaveStationName (mItemId, rename );

MRenameDialog. dismiss ();

 

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();    }


 

 

Method for deleting a radio station

DeleteStation (mItemId );

MDeleteDialog. dismiss ();

 

 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();    }

 

 

Call SharedPreferences to save the name and load ();

 

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
 
   item = new HashMap
  
   ();            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);    }
  
 


 

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 ));

 

How to obtain frequency by id

Privateint getFrequencyFromId (int id)

 

private int getFrequencyFromId(int id) {        String freq = ((HashMap
 
  ) mAdapter.getItem(id))                .get(freq);        Float fFreq = Float.parseFloat(freq);        return (int) ((fFreq * 1000));    }
 

 

Retrieve frequency names by Id

PrivateString getNameFromId (int id)

 

 private String getNameFromId(int id) {        String name = ((HashMap
 
  ) mAdapter.getItem(id))                .get(name);        return name;    }
 

 

Load Method: Clear the content and index id of the ListView list, and obtain the number of channels from SharedPreferences.

Use the for loop to add the xml data (radio station name and frequency) to the list and use SimpleAdapter to display the data in the UI.

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 });

 

Unbind FMRadioService from the onDestroy () method of the StationListActivity class.

UnbindService (osc );

protected void onDestroy() {        unbindService(osc);        mService = null;        super.onDestroy();    }


 

Related Article

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.