Android FM模块学习之四源码分析(七)
来源:互联网 发布:淘宝618活动 编辑:程序博客网 时间:2024/06/12 00:14
接上一篇,现在分析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>
- Android FM模块学习之四源码分析(七)
- Android FM模块学习之四源码分析(五)
- Android FM模块学习之四源码分析(八)
- Android FM模块学习之四源码解析(四)
- Android FM模块学习之四源码解析(一)
- Android FM模块学习之四源码解析(二)
- Android FM模块学习之四源码解析(三)
- Android FM模块学习之源码分析(六)
- Android FM模块学习之二 FM搜索频率流程
- Android FM模块学习之三 FM手动调频
- AndroidFM模块学习之四源码分析(九)
- AndroidFM模块学习之四源码分析(十)
- Android FM模块学习之一 FM启动流程
- AndroidFm模块学习之四源码解析(十一)
- FM 发射模块QN8027软件android 5.1实现分析
- Elasticsearch2.4学习(四)------源码分析之启动过程
- Elasticsearch2.4学习(四)------源码分析之启动过程
- spring源码学习之四 BeanDefinitionParserDelegate分析
- 复合主键
- java递归实现全排列
- 1-1学习MFC之消息对话框
- 技术阅读
- Live555源码在vs2010/vs2013中的编译
- Android FM模块学习之四源码分析(七)
- android 打包自定义JAR包
- SQLQuery 查询Oracle char类型返回一个字符有关问题
- linux vi保存退出命令 (如何退出vi)
- android gridview网路得到图片大小不统一出现的边框空白太大等问题
- 关于js动态添加的表单元素,提交表单后接收不到的问题
- 只需一个就够(持久【二】)续
- error PRJ0019
- java连接mongodb报错