你好,游客 登录 注册 搜索
背景:
阅读新闻

Android中按键消息的派发过程及源码分析

[日期:2012-07-02] 来源:Linux社区  作者:zjmdp [字体:大 中 小]

如果拥有焦点的View没有处理该按键消息,则继续调用event.dispatch()函数:

  1. /** 
  2.      * Deliver this key event to a {@link Callback} interface.  If this is 
  3.      * an ACTION_MULTIPLE event and it is not handled, then an attempt will 
  4.      * be made to deliver a single normal event. 
  5.      *  
  6.      * @param receiver The Callback that will be given the event. 
  7.      * @param state State information retained across events. 
  8.      * @param target The target of the dispatch, for use in tracking. 
  9.      *  
  10.      * @return The return value from the Callback method that was called. 
  11.      */  
  12.     public final boolean dispatch(Callback receiver, DispatcherState state,  
  13.             Object target) {  
  14.         switch (mAction) {  
  15.             case ACTION_DOWN: {  
  16.                 mFlags &= ~FLAG_START_TRACKING;  
  17.                 if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state  
  18.                         + ": " + this);  
  19.                 boolean res = receiver.onKeyDown(mKeyCode, this);  
  20.                 if (state != null) {  
  21.                     if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {  
  22.                         if (DEBUG) Log.v(TAG, "  Start tracking!");  
  23.                         state.startTracking(this, target);  
  24.                     } else if (isLongPress() && state.isTracking(this)) {  
  25.                         try {  
  26.                             if (receiver.onKeyLongPress(mKeyCode, this)) {  
  27.                                 if (DEBUG) Log.v(TAG, "  Clear from long press!");  
  28.                                 state.performedLongPress(this);  
  29.                                 res = true;  
  30.                             }  
  31.                         } catch (AbstractMethodError e) {  
  32.                         }  
  33.                     }  
  34.                 }  
  35.                 return res;  
  36.             }  
  37.             case ACTION_UP:  
  38.                 if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state  
  39.                         + ": " + this);  
  40.                 if (state != null) {  
  41.                     state.handleUpEvent(this);  
  42.                 }  
  43.                 return receiver.onKeyUp(mKeyCode, this);  
  44.             case ACTION_MULTIPLE:  
  45.                 final int count = mRepeatCount;  
  46.                 final int code = mKeyCode;  
  47.                 if (receiver.onKeyMultiple(code, count, this)) {  
  48.                     return true;  
  49.                 }  
  50.                 if (code != KeyEvent.KEYCODE_UNKNOWN) {  
  51.                     mAction = ACTION_DOWN;  
  52.                     mRepeatCount = 0;  
  53.                     boolean handled = receiver.onKeyDown(code, this);  
  54.                     if (handled) {  
  55.                         mAction = ACTION_UP;  
  56.                         receiver.onKeyUp(code, this);  
  57.                     }  
  58.                     mAction = ACTION_MULTIPLE;  
  59.                     mRepeatCount = count;  
  60.                     return handled;  
  61.                 }  
  62.                 return false;  
  63.         }  
  64.         return false;  
  65.     }  

该函数中主要根据相应的逻辑回调了receiver中的onKeyDown, onKeyUp,OnKeyLongPress, OnKeyMultiple函数。View中onKeyDown和onKeyUp有自己默认的处理,主要处理presse状态,长按检测,onCick回调。而OnKeyLongPress和OnKeyMultiple为空实现。对于Activity的OnKeyDown和onKeyUp函数主要实现按数字启动打电话程序( onKeyDown)以及back键的onBackPressed回调(onKeyUp)

如果按键消息在View树内部和Activity中没有被处理,就会调用到PhoneWindow的OnKeyDown和OnKeyUp函数,这是按键消息的最后处理机会,在PhoneWindow的OnKeyDown和OnKeyUp函数中主要处理了一些系统按键,例如音量键、音乐播放控制按键、照相机键、菜单键、拨号键、Search键等,具体代码就不再贴了。

以上博文主要根据《Android内核剖析》第13章以及Android源码总结而成。

linux
相关资讯       Android开发入门 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款