http://www.oschina.net/question/54100_31739
件处理流程
1)InputManager负责读取事件并把事件送到frameworks的java层
2)WindowManagerService里会有一个InputMonitor类来监听事件变化并做相应的分发处理。
3)在WindowManagerService会有一个WindowManagerPolicy来做消息拦截处理。
4)WindowManagerService会把消息发给最上面运行的窗口接收
源码分析
WindowManagerService.java主要向Android为窗口系统提供服务,把KeyEvent分发给最上层的窗口;
WindowManagerService通过InputManager提供的native接口开启了两个线程驱动做KeyEvent读取和分发给WindowManagerService管理的客户端。
mInputManager = new InputManager(context, this); mInputManager.start(); InputManager是WindowManagerService的成员变量,主要实现了读取RawEvent,分发事件给WindowManagerService; InputManager.java的native代码InputManager.cpp实现了读取和事件分发,他初始化两个线程 void InputManager::initialize() { mReaderThread = new InputReaderThread(mReader);//不断地从/dev/input/目录下面的设备文件读取事件 mDispatcherThread = new InputDispatcherThread(mDispatcher);//事件分发 } InputManager通过InputManager.Callbacks类响应回调,在回调里再调用 WindowManagerService.InputMonitor来接收事件。并在 WindowManagerService.InputMonitor.interceptKeyBeforeQueueing()和 interceptKeyBeforeDispatching()进行消息拦截处理。处理的代码如下: WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager(); /* Provides an opportunity for the window manager policy to intercept early key * processing as soon as the key has been read from the device. */ public int interceptKeyBeforeQueueing(long whenNanos, int keyCode, boolean down, int policyFlags, boolean isScreenOn) { return mPolicy.interceptKeyBeforeQueueing(whenNanos, keyCode, down, policyFlags, isScreenOn); }拦截处理的执行代码就在PhoneWindowManager.interceptKeyBeforeQueueing()方法中。详细的请参看源码