1、概述
本文通过简单的实验法获取Android事件传递
2、测试代码
测试代码很简单,随便写一个按钮监听onTouch事件,在onTouch函数中抛出一个异常,代码如下:
Button button = findViewById(R.id.btn_test);
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Log.d(TAG," onTouch "+ motionEvent);
throw new RuntimeException("event touch test");
// return false;
}
});
3、测试结果:
结果当然崩了,不会写bug的程序员不是好程序员
2019-07-15 20:55:37.297 18471-18471/com.test.playground D/EventTestActivity: onTouch MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=118.0, y[0]=29.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=642618046, downTime=642618046, deviceId=9, source=0x1002 }
2019-07-15 20:55:37.300 18471-18471/com.test.playground E/InputEventReceiver: Exception dispatching input event.
2019-07-15 20:55:37.301 18471-18471/com.test.playground E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
2019-07-15 20:55:37.302 18471-18471/com.test.playground E/MessageQueue-JNI: java.lang.RuntimeException: event touch test
at com.test.playground.EventTestActivity$1.onTouch(EventTestActivity.java:23)
at android.view.View.dispatchTouchEvent(View.java:10019)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:414)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1808)
at android.app.Activity.dispatchTouchEvent(Activity.java:3064)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:376)
at android.view.View.dispatchPointerEvent(View.java:10243)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4438)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4306)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3999)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4056)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6247)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6221)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6182)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6350)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:323)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
2019-07-15 20:55:37.303 18471-18471/com.test.playground D/AndroidRuntime: Shutting down VM
4 原理分析
4.1 事件从屏幕触摸到应用进程简述
触摸事件产生的大致原理是:用户对硬件进行操作(触摸屏)会导致这个硬件产生对应的中断。该硬件的驱动程序会处理这个中断。不同的硬件驱动程序处理的方式不同,不过最终都是将数据处理后存放进对应的/dev/input/eventX文件中。所以硬件驱动程序完成了触摸事件的数据收集。
在native层主要是通过下面3个组件来对触摸事件进行处理的,这3个组件都运行在系统服务中:
- EventHub : 它的作用是监听、读取/dev/input目录下产生的新事件,并封装成RawEvent结构体供InputReader使用。
- InputReader : 通过EventHub从/dev/input节点获取事件信息,转换成EventEntry事件加入到InputDispatcher的mInboundQueue队列中。
- InputDispatcher : 从mInboundQueue队列取出事件,转换成DispatchEntry事件加入到Connection的outboundQueue队列。然后使用InputChannel分发事件到java层
4.2 应用进程中Touch系统框架原理
- WindowInputEventReceiver: 在ViewRootImpl.setView中被初始化,当事件到来时会从native中回调onInputEvent方法到java层。是事件派发的动力所在。
- ViewRootImpl: 处理视图方面的,同Input, Window等服务交互的大管家,会在Activity显示视图时初始化
- InputStage: 被抽象成责任链模式的父类,代表着事件处理的阶段
- View, ViewGroup, DecorView: 视图树的主要组成成分
从上面调用栈中可以得到下面调用的时序图:
4.3 应用开发阶段事件传递
从3 的调用栈中可以看出
这一块有很多朋友都写过,这里就不在重复。
5 总结
本文通过onTouch的调用栈为线索,查看逐级代码。了解系统代码有助于加深对系统的理解,对于开发者而言只需要了解Activity、ViewGroup、View直接的传递。
由于能力有限,有些地方描述不够详尽,请见谅。