知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X(jué)表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷(xiāo)的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷(xiāo)工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
AndroidFM模塊學(xué)習(xí)之四源碼分析(七)
發(fā)表時(shí)間:2020-10-19
發(fā)布人:葵宇科技
瀏覽次數(shù):29
接上一篇,如今分析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);
實(shí)例化ListView對(duì)象
mStationList= (ListView) findViewById(R.id.station_list);
高低文菜單監(jiān)聽(tīng)事宜
mStationList.setOnCreateContextMenuListener(this);
ListView監(jiān)聽(tīng)每個(gè)Item事宜
mStationList.setOnItemClickListener(newOnItemClickListener()
調(diào)用mService.tune((int)((fFreq * 1000)));辦法調(diào)頻
<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));
標(biāo)題
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>
斷定電臺(tái)名字是否存在
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>
刪除電臺(tái)辦法
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>
經(jīng)由過(guò)程調(diào)用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列表內(nèi)容和索引id,大年夜SharedPreferences里獲取頻道個(gè)數(shù)
應(yīng)用for輪回將xml數(shù)據(jù)(電臺(tái)名字和頻率)添加list列表里應(yīng)用SimpleAdapter將數(shù)據(jù)顯示在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類(lèi)的onDestroy() 辦法對(duì)FMRadioService辦事解綁。
unbindService(osc);
<span style="font-family:KaiTi_GB2312;font-size:18px;">protected void onDestroy() { unbindService(osc); mService = null; super.onDestroy(); } </span>
相關(guān)案例查看更多
相關(guān)閱讀
- 百度小程序公司
- 小程序開(kāi)發(fā)聯(lián)系方式
- 百度小程序開(kāi)發(fā)
- 昆明網(wǎng)絡(luò)公司
- 英文網(wǎng)站建設(shè)公司
- 云南旅游網(wǎng)站建設(shè)
- 網(wǎng)站建設(shè)招商
- 昆明小程序設(shè)計(jì)
- 商標(biāo)
- 花農(nóng)小程序
- 網(wǎng)站優(yōu)化哪家好
- 云南省建設(shè)廳網(wǎng)站官網(wǎng)
- 小程序開(kāi)發(fā)排名前十名
- 云南網(wǎng)站建設(shè)列表網(wǎng)
- 網(wǎng)站建設(shè)靠譜公司
- 重慶網(wǎng)站建設(shè)公司
- 楚雄網(wǎng)站建設(shè)公司
- 云南網(wǎng)站建設(shè)電話(huà)
- 定制小程序開(kāi)發(fā)
- 大理網(wǎng)站建設(shè)公司
- 網(wǎng)絡(luò)公司哪家好
- 小程序被騙退款成功
- 云南做百度小程序的公司
- 云南軟件公司
- 百度小程序
- 百度排名
- 云南網(wǎng)站設(shè)計(jì)
- 網(wǎng)站建設(shè)公司地址
- 網(wǎng)站建設(shè)首頁(yè)
- 前端