背景
我们经常会遇到一种Application does not hava focused window
的ANR
异常,这种异常一般是没有焦点窗口FocusedWindow导致,且这类异常只会发生在key事件的派发,因为key事件是需要找到一个焦点窗口然后再派发,而触摸事件只需要找到当前显示的窗口即可
焦点窗口设定
WMS只管理窗口,无法确定是否有窗口盖住当前画面
SurfaceFlinger管理显示,最贴近于用户看到的画面,可以知道可以知道是否有窗口盖住当前画面,根据真实的显示窗口设置对应的window信息给InputDispatcher
关键日志
window
在dumpsys window中查看mCurrentFocus和mFocusedApp
mCurrentFocus=Window{f96644 u0 NotificationShade}
mFocusedApp=ActivityRecord{e9566ee u0 com.android.launcher3/.uioverrides.QuickstepLauncher} t12}
mCurrentFocus
指的是当前的焦点窗口mFocusedApp
指的是当前的焦点Activity
查看有没有LAST ANR
WINDOW MANAGER LAST ANR (dumpsys window lastanr)
<no ANR has occurred since boot>
这里没有LAST ANR,如果有,mCurrentFocus会显示null
SurfaceFlinger
在dumpsys SurfaceFlinger中查看 HWC layers
Display 4619827259835644672 (active) HWC layers:
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Layer name
Z | Window Type | Comp Type | Transform | Disp Frame (LTRB) | Source Crop (LTRB) | Frame Rate (Explicit) (Seamlessness) [Focused]
---------------------------------------------------------------------------------------------------------------------------------------------------------------
com.example.mysystemdialog/com.example.mysystemdialog.MainActivity#118
rel 0 | 1 | CLIENT | 0 | 0 0 1440 2960 | 0.0 0.0 1440.0 2960.0 | [*]
---------------------------------------------------------------------------------------------------------------------------------------------------------------
StatusBar#75
rel 0 | 2000 | CLIENT | 0 | 0 0 1440 84 | 0.0 0.0 1440.0 84.0 | [ ]
---------------------------------------------------------------------------------------------------------------------------------------------------------------
NavigationBar0#74
rel 0 | 2019 | CLIENT | 0 | 0 2792 1440 2960 | 0.0 0.0 1440.0 168.0 | [ ]
---------------------------------------------------------------------------------------------------------------------------------------------------------------
[Focused]
这一列有带[*]号,则说明是焦点窗口
input
在dumpsys input中查看FocusedApplications和FocusedWindows
FocusedApplications:
displayId=0, name='ActivityRecord{e9566ee u0 com.android.launcher3/.uioverrides.QuickstepLauncher} t12}', dispatchingTimeout=5000ms
FocusedWindows:
displayId=0, name='f96644 NotificationShade'
如果发生ANR,焦点窗口以dumpsys input为主
Input Dispatcher State at time of last ANR:
ANR:
Time:......
Reason:......
Window:......
FocusedApplications:......
FocusedWindows: <none>
event log
05-18 19:22:55.806 580 607 I input_focus: [Focus request f96644 NotificationShade,reason=UpdateInputWindows]
05-18 19:22:55.837 580 675 I input_focus: [Focus leaving e225d94 com.android.launcher3/com.android.launcher3.uioverrides.QuickstepLauncher (server),reason=Waiting for window because NO_WINDOW]
05-18 19:22:55.855 580 675 I input_focus: [Focus entering f96644 NotificationShade (server),reason=Window became focusable. Previous reason: NOT_VISIBLE]
request
和 entering
正常情况下是一一对应,打印了entering
则表示真正的焦点已经进入到对应的窗口
发生Application does not hava focused window
时,一般request
有打印,我们可以通过是否有entering
的打印来分析
1.entering
部分有打印,代表焦点已经在input里面,但是仍然有ANR,就需要从input等方面分析
2.entering
部分未打印,代表input没有被触发焦点窗口设置到input,需排查SurfaceFlinger或WMS
总结
分析方法
-
确认焦点所在的窗口
- 在Event.log中找input_focus
- 在dumpsys SurfaceFlinger中查看 HWC layers
- 在dumpsys window中查看mCurrentFocus和mFocusedApp
- 在dumpsys input中查看FocusedApplications和FocusedWindows
-
对比正异常窗口焦点是否在同一个窗口
对比正异常log或者dump信息 -
分析事件是否有正常派发
假如有正常派发在看看下面的步骤 -
对比应用View树的绘制
adb shell dumpsys activity -v top > top.txt
焦点且事件派发正常,但是有可能点击View没有反应,没有点击到正确View上,或者有透明窗口,这时就要看看正常和异常的View区别在哪,看起是否有不同点文章来源:https://www.toymoban.com/news/detail-620633.html -
寻找其他异常点
比如一些异常log文章来源地址https://www.toymoban.com/news/detail-620633.html
到了这里,关于android 窗口焦点介绍的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!