Android FM模块学习之四源码分析(七)
接上一篇,现在分析android\vendor\qcom\opensource\fm\fmapp2\src\com\caf\fmradio\StationListActivity.java
protectedvoid onCreate(Bundle savedInstanceState)方法里
绑定FMRadioService服务
bindService((newIntent()).setClass(this, FMRadioService.class), osc, 0);
实例化ListView对象
mStationList= (ListView) findViewById(R.id.station_list);
上下文菜单监听事件
mStationList.setOnCreateContextMenuListener(this);
ListView监听每个Item事件
mStationList.setOnItemClickListener(newOnItemClickListener()
调用mService.tune((int)((fFreq * 1000)));方法调频
<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(AdapterView<?> arg0, View arg1, int arg2, long arg3) { String freq = ((HashMap<String, 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>
上下文菜单
public void onCreateContextMenu(ContextMenumenu, View v,ContextMenuInfo 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));
标题
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 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; } }</span>
判断电台名字是否存在
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>
重命名保存方法
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>
删除电台方法
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>
通过调用SharedPreferences保存名字后加载 load();
<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, String>(); 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>
方法
privatevoid saveStationName(int id, String name)
editor.putString(FMRadio.STATION_NAME+ (stationIndex.intValue()),
privatevoid deleteStation(int id) 删除方法,删除名字和频率
editor.remove(FMRadio.STATION_NAME +(stationIndex));
editor.remove(FMRadio.STATION_FREQUENCY +(stationIndex));
以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)) .get("freq"); Float fFreq = Float.parseFloat(freq); return (int) ((fFreq * 1000)); }</span>
以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>
加载方法,清除ListView列表内容和索引id,从SharedPreferences里获取频道个数
使用for循环将xml数据(电台名字和频率)添加list列表里使用SimpleAdapter将数据显示在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 });
在StationListActivity类的onDestroy() 方法对FMRadioService服务解绑。
unbindService(osc);
<span style="font-family:KaiTi_GB2312;font-size:18px;">protected void onDestroy() { unbindService(osc); mService = null; super.onDestroy(); } </span>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。