http://blog.csdn.net/elfylin/article/details/8008763
一. 接口
KeyEvent.Callback和View.OnKeyListener
二.
流程
a.Activity.dispatchKeyEvent
a.1.win.superDispatchKeyEvent(win=getWindow)
Window.superDispatchKeyEvent->PhoneWindow.superDispatchKeyEvent
a.2mDecor.superDispatchKeyEvent
(mDecor=FrameLayout)
PhoneWindow.mDecor.superDispatchKeyEvent
a.3view.
dispatchKeyEvent
(Window.dispatchKeyEvent)
View.dispatchKeyEvent(View=mDecor)
b.event.dispatch
mOnKeyListener.onKey
三.Event
a. who call onKeyDown?
b. who call
dispatchKeyevent
c.
dispatchKeyEvent的实现在PhoneWindow.dispatchKeyEvent
PhoneWindows重载了View的dispatchKeyEvent和KeyEventCallback的OnKeyXX
首先调用dispatchKeyEvent,如果返回false才调用onKeyXX
在其父类View的dispatchKeyEvent,会调用KeyListener。
四.优先级
1.当一个Activity中收到事件,首先查看自己的decor是否有焦点,
1.1如果有调用它的dispatchKeyEvent,(即调用它的keylistener);
1.2否则,获取焦点窗口,调用焦点窗口的dispatchKeyEvent;
2.如果上面都为false,就调用自己的onkeydown和onkeyup事件。
五 入口
HelloworldActivity.onKeyDown(int, KeyEvent) line:
25
KeyEvent.dispatch(KeyEvent$Callback, KeyEvent$DispatcherState, Object)
line: 2555
HelloworldActivity(Activity).dispatchKeyEvent(KeyEvent) line:
2358
HelloworldActivity.dispatchKeyEvent(KeyEvent) line:
29
PhoneWindow$DecorView.dispatchKeyEvent(KeyEvent) line:
1806
ViewRootImpl.deliverKeyEventPostIme(KeyEvent, boolean) line:
3328
ViewRootImpl.handleFinishedEvent(int, boolean) line:
3301
ViewRootImpl.handleMessage(Message) line:
2461
ViewRootImpl(Handler).dispatchMessage(Message) line:
99
Looper.loop() line: 137
ActivityThread.main(String[]) line:
4444
Method.invokeNative(Object, Object[], Class, Class[], Class, int,
boolean) line: not available [native method]
Method.invoke(Object, Object...)
line: 511
ZygoteInit$MethodAndArgsCaller.run() line:
787
ZygoteInit.main(String[]) line: 554
NativeStart.main(String[])
line: not available [native method]
原文地址:http://www.cnblogs.com/daishuguang/p/3718024.html