在安卓上获得某个Window画面所属包名packageName和用户userId的方法
1,用到的工具如下:
adb
androidSDK里的monitor工具
adb shell dumpsys window animator
adb shell dumpsys window命令
jdk 1.8
已在安卓14模拟器上测试通过。
以AOSP的launcher中的map图层为例,如何确认map图层所属包名和用户?
实施步骤如下。
1),首先用androidSDK里带的monitor工具,查看对应window名称
(建议本地jdk版本是java1.8,否则可能打开失败)
java -version输出如下:
openjdk version "1.8.0_412"
OpenJDK Runtime Environment (build 1.8.0_412-8u412-ga-1~22.04.1-b08)
OpenJDK 64-Bit Server VM (build 25.412-b08, mixed mode)
打开monitor工具:
sdk/tools/lib/monitor-x86_64/monitor目录下打开monitor工具: ./monitor
打开monitor ui界面后,点击右上角的Hierarchy View功能,在左侧的windows列表中,逐个确认和对比你想要查看的UI,确认下图里编号4的画面是否是你想要的UI画面。
我们可以看到,这个UI图层跟我们在真实设备上的UI一模一样,这就是我们需要的图层。
看左边的列表,可以看到window名称为:
com.android.car.mapsplaceholder/com.android.car.mapsplaceholder.MapsPlaceholderActivity
2,通过dumpsys window animator命令,查看window对象的hash
adb shell dumpsys window animator
WINDOW MANAGER ANIMATOR STATE (dumpsys window animator)Display{#8 state=ON size=968x692 ROTATION_0}:Window #0: WindowStateAnimator{dbae469 com.android.car.cluster.home/com.android.car.cluster.home.ClusterHomeActivity}Window #1: WindowStateAnimator{c616eee com.android.car.cluster.home/com.android.car.cluster.home.FakeFreeNavigationActivity}Display{#1 state=ON size=968x792 ROTATION_0}:Window #0: WindowStateAnimator{f5cef8f com.android.car.cluster.osdouble/com.android.car.cluster.osdouble.ClusterOsDoubleActivity}Display{#3 state=ON size=1408x792 ROTATION_0}:Window #0: WindowStateAnimator{329c21c com.android.systemui/com.android.systemui.car.userpicker.UserPickerActivity}Window #1: WindowStateAnimator{f945e25 InputMethod}Display{#2 state=ON size=1408x792 ROTATION_0}:Window #0: WindowStateAnimator{c798bfa com.android.car.multidisplay/com.android.car.multidisplay.launcher.LauncherActivity}Window #1: WindowStateAnimator{5c2f5ab InputMethod}Window #2: WindowStateAnimator{81ee408 SystemUIOverlayWindow}Window #3: WindowStateAnimator{4d1b7a1 HeadsUpNotification}Window #4: WindowStateAnimator{ac46dc6 BottomCarSystemBar}Window #5: WindowStateAnimator{c394187 ScreenDecorOverlay}Window #6: WindowStateAnimator{bc238b4 ScreenDecorOverlayBottom}Display{#0 state=ON size=1848x792 ROTATION_0}:Window #0: WindowStateAnimator{e312cdd com.google.android.car.kitchensink/com.google.android.car.kitchensink.KitchenSinkActivity}Window #1: WindowStateAnimator{689a052 com.android.car.radio/com.android.car.radio.RadioActivity}Window #2: WindowStateAnimator{8de6f23 com.android.car.carlauncher/com.android.car.carlauncher.CarLauncher}Window #3: WindowStateAnimator{4a42c20 com.android.car.mapsplaceholder/com.android.car.mapsplaceholder.MapsPlaceholderActivity}Window #4: WindowStateAnimator{18b9d9 InputMethod}Window #5: WindowStateAnimator{9fd6f9e ShellDropTarget}Window #6: WindowStateAnimator{b1eda7f }Window #7: WindowStateAnimator{fefea4c TopCarSystemBar}Window #8: WindowStateAnimator{5521a95 SystemUIOverlayWindow}Window #9: WindowStateAnimator{c4ce7aa HeadsUpNotification}Window #10: WindowStateAnimator{2d09f9b BottomCarSystemBar}Window #11: WindowStateAnimator{575f38 ScreenDecorOverlay}Window #12: WindowStateAnimator{186cb11 ScreenDecorOverlayBottom}mCurrentTime=433030 (321127 ms ago)
我们可以看到com.android.car.mapsplaceholder/com.android.car.mapsplaceholder.MapsPlaceholderActivity对应的hash号为4a42c20
Window #3: WindowStateAnimator{4a42c20 com.android.car.mapsplaceholder/com.android.car.mapsplaceholder.MapsPlaceholderActivity}
3,通过dumpsys window命令,查看4a42c20 window对应的userId和包名
adb shell dumpsys window >~/dump_window.txt
查看4a42c20 window对应的dump信息,我们可以看到u10和package=com.android.car.mapsplaceholder信息。
Window #9 Window{85b2366 u10 com.android.car.mapsplaceholder/com.android.car.mapsplaceholder.MapsPlaceholderActivity}:mDisplayId=0 rootTaskId=1000031 mSession=Session{a67f8cb 4470:u10a10134} mClient=android.os.BinderProxy@aa13fc1mOwnerUid=1010134 showForAllUsers=false package=com.android.car.mapsplaceholder appop=NONEmAttrs={(0,0)(fillxfill) sim={adjust=pan forwardNavigation} ty=BASE_APPLICATION wanim=0x10302fefl=LAYOUT_IN_SCREEN LAYOUT_INSET_DECOR SPLIT_TOUCH HARDWARE_ACCELERATED DRAWS_SYSTEM_BAR_BACKGROUNDSpfl=NO_MOVE_ANIMATION FORCE_DRAW_STATUS_BAR_BACKGROUND USE_BLAST FIT_INSETS_CONTROLLEDbhv=DEFAULTfitSides=}Requested w=1444 h=620 mLayoutSeq=277mHasSurface=true isReadyForDisplay()=true mWindowRemovalAllowed=falseWindowStateAnimator{4a42c20 com.android.car.mapsplaceholder/com.android.car.mapsplaceholder.MapsPlaceholderActivity}:Surface: shown=true layer=0 alpha=1.0 rect=(0.0,0.0) transform=(1.0, 0.0, 0.0, 1.0)mForceSeamlesslyRotate=false seamlesslyRotate: pending=null isOnScreen=trueisVisible=truekeepClearAreas: restricted=[], unrestricted=[]mPrepareSyncSeqId=0
最终获得我们需要的信息,User是10,packageName是com.android.car.mapsplaceholder
--------------------------------------------------End line-----------------------------------------