android 窗口焦点介绍
背景
我们经常会遇到一种Application does not hava focused window
的ANR
异常,这种异常一般是没有焦点窗口FocusedWindow导致,且这类异常只会发生在key事件的派发,因为key事件是需要找到一个焦点窗口然后再派发,而触摸事件只需要找到当前显示的窗口即可
关键日志
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
input
在dumpsys input中查看FocusedApplications和FocusedWindows
FocusedApplications:displayId=0, name='ActivityRecord{e9566ee u0 com.android.launcher3/.uioverrides.QuickstepLauncher} t12}', dispatchingTimeout=5000msFocusedWindows:displayId=0, name='f96644 NotificationShade'
如果发生ANR,焦点窗口以dumpsys input为主
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