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





郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。