InputEventReceiver
public InputEventReceiver(InputChannel inputChannel, Looper looper) {
if (inputChannel == null) {
throw new IllegalArgumentException("inputChannel must not be null");
}
if (looper == null) {
throw new IllegalArgumentException("looper must not be null");
}
mInputChannel = inputChannel;
mMessageQueue = looper.getQueue();
//调用native代码初始化
mReceiverPtr = nativeInit(new WeakReference<InputEventReceiver>(this),
inputChannel, mMessageQueue);
mCloseGuard.open("dispose");
}
InputEventReceiver nativeInit()方法的调用,这里系统 native 层就会将这个InputEventReceiver实例记录下来,每当有事件到达时就会通过inputChannel管道派发到这个实例上
onInputEvent:7195, ViewRootImpl$WindowInputEventReceiver (android.view)
dispatchInputEvent:186, InputEventReceiver (android.view) // // Called from native code.
nativePollOnce:-1, MessageQueue (android.os) //底层事件上报
next:326, MessageQueue (android.os)
loop:160, Looper (android.os)
main:6669, ActivityThread (android.app)
invoke:-1, Method (java.lang.reflect)
run:493, RuntimeInit$MethodAndArgsCaller (com.android.internal.os)
main:858, ZygoteInit (com.android.internal.os)
- 首先,当我们触摸屏幕时,通过Android消息机制,从Looper从MessageQueue中取出该事件,发送给WindowInputEventReceiver。
- WindowInputEventReceiver是ViewRootImpl的内部类,通过enqueueInputEvent方法,将输入事件加入输入事件队列中,并进行处理和转发。
- ViewPostImeInputStage收到输入事件,将事件传递给DecorView的dispatchPointerEvent()方法(是View的方法)
- dispatchPointerEvent()方法通过DecorView中的dispatchTouchEvent()方法,调用了Activity的dispatchTouchEvent()方法
sendMessageAtTime:4062, ViewRootImpl$ViewRootHandler (android.view)
sendMessageDelayed:667, Handler (android.os)
postDelayed:469, Handler (android.os)
postDelayed:16707, View (android.view)
onTouchEvent:13837, View (android.view)
onTouchEvent:10064, TextView (android.widget)
dispatchTouchEvent:12513, View (android.view)
dispatchTransformedTouchEvent:3030, ViewGroup (android.view)
dispatchTouchEvent:2662, ViewGroup (android.view)
dispatchTransformedTouchEvent:3030, ViewGroup (android.view)
dispatchTouchEvent:2662, ViewGroup (android.view)
dispatchTransformedTouchEvent:3030, ViewGroup (android.view)
dispatchTouchEvent:2662, ViewGroup (android.view)
dispatchTransformedTouchEvent:3030, ViewGroup (android.view)
dispatchTouchEvent:2662, ViewGroup (android.view)
dispatchTransformedTouchEvent:3030, ViewGroup (android.view)
dispatchTouchEvent:2662, ViewGroup (android.view)
dispatchTransformedTouchEvent:3030, ViewGroup (android.view)
dispatchTouchEvent:2662, ViewGroup (android.view)
dispatchTransformedTouchEvent:3030, ViewGroup (android.view)
dispatchTouchEvent:2662, ViewGroup (android.view)
superDispatchTouchEvent:440, DecorView (com.android.internal.policy)
superDispatchTouchEvent:1830, PhoneWindow (com.android.internal.policy)
dispatchTouchEvent:3400, Activity (android.app)
dispatchTouchEvent:69, i (a.b.o)
dispatchTouchEvent:69, i (a.b.o)
dispatchTouchEvent:398, DecorView (com.android.internal.policy)
dispatchPointerEvent:12752, View (android.view)
processPointerEvent:5106, ViewRootImpl$ViewPostImeInputStage (android.view)
onProcess:4909, ViewRootImpl$ViewPostImeInputStage (android.view)
deliver:4426, ViewRootImpl$InputStage (android.view)
onDeliverToNext:4479, ViewRootImpl$InputStage (android.view)
forward:4445, ViewRootImpl$InputStage (android.view)
forward:4585, ViewRootImpl$AsyncInputStage (android.view)
apply:4453, ViewRootImpl$InputStage (android.view)
apply:4642, ViewRootImpl$AsyncInputStage (android.view)
deliver:4426, ViewRootImpl$InputStage (android.view)
onDeliverToNext:4479, ViewRootImpl$InputStage (android.view)
forward:4445, ViewRootImpl$InputStage (android.view)
apply:4453, ViewRootImpl$InputStage (android.view)
deliver:4426, ViewRootImpl$InputStage (android.view)
deliverInputEvent:7092, ViewRootImpl (android.view)
doProcessInputEvents:7061, ViewRootImpl (android.view)
enqueueInputEvent:7022, ViewRootImpl (android.view)//将输入事件加入输入事件队列中,并进行处理和转发
onInputEvent:7195, ViewRootImpl$WindowInputEventReceiver (android.view)
dispatchInputEvent:186, InputEventReceiver (android.view)//native层上报事件
nativePollOnce:-1, MessageQueue (android.os)
next:326, MessageQueue (android.os)
loop:160, Looper (android.os)
main:6669, ActivityThread (android.app)
invoke:-1, Method (java.lang.reflect)
run:493, RuntimeInit$MethodAndArgsCaller (com.android.internal.os)
main:858, ZygoteInit (com.android.internal.os)
事件传递规则
当Activity接受到点击事件后,会传递给Window再传递给DecorView,DecorView就是一个ViewGroup,他的dispatchTouchEvent就会被调用,如果这个ViewGroup的onInterceptTouchEvent方法返回true,就表示它要拦截事件,事件就会交给这个ViewGroup来处理;如果这个ViewGroup的onInterceptTouchEvent返回false,表示不拦截当前事件,这时,当前事件就会继续传递给它的子元素,子元素的dispatchTouchEvent方法就会被调用,如此反复直到事件被最终处理。(ViewGroup一般默认不拦截事件)