AndroidFm模块学习之四源码解析(十一)

上一篇,接下来看看android\vendor\qcom\opensource\fm\fmapp2\src\com\caf\fmradio\FmTags.java

当点击FMRadio.java菜单的全部频道选项,跳转到FmTags.java类

定义了一个ListView控件和一个简单适配器

private ListView la;

private ArrayAdapter<String> adapter;

使用Handler刷新UI界面

private final Handler mHandler = newHandler();

使用迭代器private Iterator itr;

将FmSharedPreferences类取出数据迭代化,再使用循环添加到字符数组里,使用适配器显示。

 final Runnable mDisplayTagList = new Runnable() {
            public void run() {
                   String[] tags;
                   int l = 0;

                   tags = new String[FmSharedPreferences.num_tags];
                   for(int i = 0; i < FmSharedPreferences.MAX_NUM_TAG_TYPES; i++) {
                       if (FmSharedPreferences.tagList[i] != null) {
                          itr = FmSharedPreferences.tagList[i].iterator();
                          while(itr.hasNext())
                                tags[l++] = ((FmSharedPreferences.TAG_NAMES[i]) + "\n" +
                                              "\t" + (String)itr.next());
                       }
                   }
                   adapter = new ArrayAdapter(la.getContext(), android.R.layout.simple_list_item_1, tags);
                   la.setAdapter(adapter);
            }
       };

再分析一个FM耳机按键的类 android\vendor\qcom\opensource\fm\fmapp2\src\com\caf\fmradio\FMMediaButtonIntentReceiver.java

手机耳机控制音量或者其它,通过静态注册接收action(“android.intent.action.MEDIA_BUTTON”)

按键监听事件

KeyEvent event =(KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

监听是否是头戴式耳机和点击按下去的监听

intkeycode = event.getKeyCode();

intkey_action = event.getAction();

发送一个附带Intent.EXTRA_KEY_EVENT广播去FMRadioService实现接听实现一些功能。

KeyEvent.KEYCODE_HEADSETHOOK:监听是否是头戴式耳机

KeyEvent.ACTION_DOWN:监听是否按下键

KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:暂停

KeyEvent.KEYCODE_MEDIA_PLAY 播放键

KeyEvent.KEYCODE_MEDIA_NEXT:短按=播放下一首音乐,长按=当前音乐快进

KeyEvent.KEYCODE_MEDIA_PREVIOUS:短按=播放上一首音乐,长按=当前音乐快退

欧标耳机才能测试出监听事件

public class FMMediaButtonIntentReceiver extends BroadcastReceiver {

private static final String TAG = "FMMediaButtonIntentReceiver";
public static final String FM_MEDIA_BUTTON = "com.caf.fmradio.action.MEDIA_BUTTON";
public static final String AUDIO_BECOMING_NOISY = "com.caf.fmradio.action.AUDIO_BECOMING_NOISY";
public void onReceive(Context context, Intent intent) {
       String action = intent.getAction();
       if ((action != null) && action.equals(AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
           Log.d(TAG, "ACTION_AUDIO_BECOMING_NOISY intent received for ACTION_HEADSET_PLUG");
           Intent i = new Intent(AUDIO_BECOMING_NOISY); context.sendBroadcast(i);
       } else if ((action != null) && action.equals("android.intent.action.MEDIA_BUTTON")) {
           KeyEvent event = (KeyEvent)
                    intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

           if (event == null) {
               return;
           }
           int keycode = event.getKeyCode();
           int key_action = event.getAction();
           if (((KeyEvent.KEYCODE_HEADSETHOOK == keycode) &&
               (key_action == KeyEvent.ACTION_DOWN)) ||
               (KeyEvent.KEYCODE_MEDIA_PAUSE == keycode) ||
               (KeyEvent.KEYCODE_MEDIA_PLAY == keycode)) {

               Log.d(TAG, "ACTION_MEDIA_BUTTON intent received for ACTION_DOWN");
               Intent i = new Intent(FM_MEDIA_BUTTON);
               i.putExtra(Intent.EXTRA_KEY_EVENT, event);
               context.sendBroadcast(i);
           }
       }
   }
时间: 2024-10-07 04:38:17

AndroidFm模块学习之四源码解析(十一)的相关文章

Android FM模块学习之四源码解析(二)

上一章我们了解了FM主activity:FMRadio.java,若没查看的,请打开链接Android FM模块学习之四源码解析(一) 查看fmradio.java源码注释.接下来我们来看看FM重要的一个类:FMRadioService.java 由上一章我们已经知道,打开FM时,在OnStart函数中会bindToService来开启服务, public boolean bindToService(Context context, ServiceConnection callback) { L

Android FM模块学习之四源码解析(三)

由于最近一直忙项目,没有时间来更新文档,今天抽空来写一点,希望大家可以学习使用! 这一章当然还是来分析FM模块的源码.FmReceiver.java publicFmReceiver(String devicePath,FmRxEvCallbacksAdaptor callback) throwsInstantiationException { mControl = new FmRxControls(); mRxEvents = new FmRxEventListner(); //registe

Android FM模块学习之四源码解析(一)

前一章我们了解了FM手动调频,接下来我们要分析FM模块用到的源码.此源码是基于高通平台的,别的平台都大同小异,只不过是平台自己作了些小改动而已. 首先要看的当然是主activity, FMRadio.java fmradio类启动FMRadioService.java类调用FmSharedPreferences类进行存储数据,PresetStation调整频率 setVolumeControlStream(AudioManager.STREAM_MUSIC);音乐回放即媒体音量 LoadedDa

Android FM模块学习之四源码解析(四)

我今天想分享的是FM模块的保存方法,即FmSharedPreferences.java FmSharedPreferences(Context context)在构造方法中加载Load()方法, public void  Load(){       Log.d(LOGTAG, "Load preferences ");       if(mContext == null)       {          return;       } SharedPreferences sp = m

AndroidFM模块学习之四源码分析(九)

接上一篇,接下来我们看看android\vendor\qcom\opensource\fm\fmapp2\src\com\caf\fmradio\PresetList.java 定义一个List列表List<PresetStation>mPresetList = new ArrayList<PresetStation>(); 同步电台数量 public synchronized int getStationCount(){ return mPresetList.size(); }

AndroidFM模块学习之四源码分析(十)

接上一篇,今天我们来看看android\vendor\qcom\opensource\fm\qcom\fmradio\FmRxControls.java / * *打开FM Rx / Tx. * Rx = 1和Tx = 2 * / public void fmOn(int fd, int device) { int re; FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_STATE, device ); setAudioPa

Android FM模块学习之四源码分析(3)

接着看FM模块的其他几个次要的类的源码.这样来看FM上层的东西不是太多. 请看android\vendor\qcom\opensource\fm\fmapp2\src\com\caf\fmradio\Settings.java protected void onCreate(BundlesavedInstanceState) 从FMRadio.java用使用Intent跳转携带参数过来,在onCreate获取携带数据. protected void onCreate(Bundle savedIn

Android FM模块学习之四源码学习(2)

前几章我们分析了FM模块的几个主要的类文件,今天要分析的是:FMTransceiver.java   // 某些工程中名称为FMRadioService.java public class FmTransceiver { /* Primary FM States : * FM will be in one of the 4 states at any point of time * '0' - FMState_Turned_Off * '1' - FMState_Rx_Turned_On * '

Android FM模块学习之四源码分析(五)

前几章我们分析了FM模块的几个主要的类文件,今天要分析的是:FMTransceiver.java public class FmTransceiver { /* Primary FM States : * FM will be in one of the 4 states at any point of time * '0' - FMState_Turned_Off * '1' - FMState_Rx_Turned_On * '2' - FMState_Tx_Turned_On * '3' -