HierarchyViewer源代码理解(三)

本文详细阐述了如何通过ADB命令行连接到ViewServer,并使用Java程序与之通信,展示了如何获取当前Android设备上的活动Activity及其控件信息。通过发送特定命令并解析返回的Parcel对象,实现对HierarchyViewer的深入研究。
摘要由CSDN通过智能技术生成

上一篇文章中提到HierarchyViewer中,命令送与接收的机制并不是很清楚,篇文章就主要对这行研究

 

首先,通adb命令行,接到ViewServer(真机的需要root限才能够连接)

如下

1.adb -e forward tcp:4939 tcp:4939 Android设备4939端口映射到pc4939端口上

2.adb -e shell service call window 3 看服是否开启

     开启:Result:Parcel(00000000 00000001   '........')

     未开启:Result:Parcel(00000000 00000000   '........')

3. adb-e shell service call window 1 i32 4939

 

接下来,需要创建一个java程序

import java.io.*;

import java.net.*;

public class test {


public static void main(String[] args) {

// TODO Auto-generated method stub

Socket socket = new Socket();

    try {

socket.connect(new InetSocketAddress("127.0.0.1", 4939),40000);

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8"));

    out.write("LIST"); //sent command

    out.newLine();

    out.flush();

    String context="";

    String line;

    while ((line = in.readLine()) != null) {

     

    if ("DONE.".equalsIgnoreCase(line)) { //$NON-NLS-1$

    break;

    }

    context+=line+"\r\n";

    System.out.println(context);

    }       

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}


由于socket方面,我也是一知半解,就不详细解释了,如上程序运行结果如下,列出了当前Android设备上所有活动的activity

b3fe4fa8 com.android.systemui.ImageWallpaper


b3fe4fa8 com.android.systemui.ImageWallpaper

b4085080 com.android.launcher/com.android.launcher2.Launcher


b3fe4fa8 com.android.systemui.ImageWallpaper

b4085080 com.android.launcher/com.android.launcher2.Launcher

b4020240 KeyguardScrim


b3fe4fa8 com.android.systemui.ImageWallpaper

b4085080 com.android.launcher/com.android.launcher2.Launcher

b4020240 KeyguardScrim

b40f0b30 Keyguard


b3fe4fa8 com.android.systemui.ImageWallpaper

b4085080 com.android.launcher/com.android.launcher2.Launcher

b4020240 KeyguardScrim

b40f0b30 Keyguard

b40b2bf0 StatusBar


前面的数字是一个16进制的哈希值,利用这个哈希值,通过DUMP hashCode这个命令,就可以获得该activity中控件的所有信息,以Keyguar举例。打印结果如下

com.android.keyguard.KeyguardViewManager$ViewManagerHost@b3e01ce0 drawing:mForeground=4,null padding:mForegroundPaddingBottom=1,0 padding:mForegroundPaddingLeft=1,0 padding:mForegroundPaddingRight=1,0 padding:mForegroundPaddingTop=1,0 drawing:mForegroundInPadding=4,true measurement:mMeasureAllChildren=5,false drawing:mForegroundGravity=3,119 events:mLastTouchDownX=5,262.0 events:mLastTouchDownTime=1,0 events:mLastTouchDownY=5,645.0 events:mLastTouchDownIndex=2,-1 mGroupFlags_CLIP_CHILDREN=3,0x1 mGroupFlags_CLIP_TO_PADDING=3,0x2 mGroupFlags=7,2244691 layout:mChildCountWithTransientState=1,0 focus:getDescendantFocusability()=24,FOCUS_BEFORE_DESCENDANTS drawing:getPersistentDrawingCache()=9,SCROLLING drawing:isAlwaysDrawnWithCacheEnabled()=4,true isAnimationCacheEnabled()=4,true drawing:isChildrenDrawingOrderEnabled()=5,false drawing:isChildrenDrawnWithCacheEnabled()=5,false measurement:mMeasuredWidth=3,480 measurement:mMinHeight=1,0 measurement:mMinWidth=1,0 measurement:mMeasuredHeight=3,800 padding:mPaddingBottom=1,0 padding:mPaddingLeft=1,0 padding:mPaddingRight=1,0 padding:mPaddingTop=1,0 layout:mLeft=1,0 drawing:mLayerType=4,NONE mPrivateFlags_DRAWING_CACHE_INVALID=3,0x0 mPrivateFlags_DRAWN=4,0x20 mPrivateFlags_DIRTY=8,0x200000 mPrivateFlags=11,-2128605904 mID=5,NO_ID layout:mRight=3,480 scrolling:mScrollX=1,0 scrolling:mScrollY=1,0 mSystemUiVisibility_SYSTEM_UI_FLAG_VISIBLE=3,0x0 mSystemUiVisibility=7,2097152 layout:mBottom=3,800 layout:mTop=1,0 padding:mUserPaddingBottom=1,0 padding:mUserPaddingEnd=11,-2147483648 padding:mUserPaddingLeft=1,0 padding:mUserPaddingRight=1,0 padding:mUserPaddingStart=11,-2147483648 mViewFlags=9,402653320 drawing:getAlpha()=3,1.0 layout:getBaseline()=2,-1 accessibility:getContentDescription()=4,null getFilterTouchesWhenObscured()=5,false layout:getHeight()=3,800 accessibility:getImportantForAccessibility()=3,yes accessibility:getLabelFor()=2,-1 layout:getLayoutDirection()=22,RESOLVED_DIRECTION_LTR layout_horizontalWeight=3,0.0 layout_flags_FLAG_LAYOUT_IN_SCREEN=5,0x100 layout_flags_FLAG_FORCE_NOT_FULLSCREEN=5,0x800 layout_flags_FLAG_LAYOUT_INSET_DECOR=7,0x10000 layout_flags_FLAG_ALT_FOCUSABLE_IM=7,0x20000 layout_flags_FLAG_SHOW_WALLPAPER=8,0x100000 layout_flags=7,1247488 layout_type=13,TYPE_KEYGUARD layout_verticalWeight=3,0.0 layout_x=1,0 layout_y=1,0 layout:layout_height=12,MATCH_PARENT layout:layout_width=12,MATCH_PARENT drawing:getPivotX()=3,0.0 drawing:getPivotY()=3,0.0 layout:getRawLayoutDirection()=3,LTR text:getRawTextAlignment()=7,GRAVITY text:getRawTextDirection()=7,INHERIT drawing:getRotation()=3,0.0 drawing:getRotationX()=3,0.0 drawing:getRotationY()=3,0.0 drawing:getScaleX()=3,1.0 drawing:getScaleY()=3,1.0 getScrollBarStyle()=14,INSIDE_OVERLAY drawing:getSolidColor()=1,0 getTag()=4,null text:getTextAlignment()=7,GRAVITY text:getTextDirection()=12,FIRST_STRONG drawing:getTranslationX()=3,0.0 drawing:getTranslationY()=3,0.0 getVisibility()=4,GONE layout:getWidth()=3,480 drawing:getX()=3,0.0 drawing:getY()=3,0.0 focus:hasFocus()=5,false layout:hasTransientState()=5,false isActivated()=5,false isClickable()=5,false drawing:isDrawingCacheEnabled()=5,false isEnabled()=4,true focus:isFocusable()=5,false isFocusableInTouchMode()=5,false focus:isFocused()=5,false isHapticFeedbackEnabled()=4,true isHovered()=5,false isInTouchMode()=4,true layout:isLayoutRtl()=5,false drawing:isOpaque()=5,false isSelected()=5,false isSoundEffectsEnabled()=4,true drawing:willNotCacheDrawing()=5,false drawing:willNotDraw()=4,true 


到这里,HierarchyViewer的大致原理框架已经清楚了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值