android|adb 打印activity堆栈

文章来源:http://www.k-beta.com/dump_activity.html
更多adb命令:https://blog.csdn.net/u013750244/article/details/122071015
使用adb查看当前手机的相关内容
adb shell dumpsys activity
可以得到如下内容:

ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents) * PendingIntentRecord{42b05f20 com.android.vending startService} ... ... ... ...ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts) Historical broadcasts [foreground]: #0: BroadcastRecord{430d2fb8 u-1 android.intent.action.TIME_TICK} act=android.intent.action.TIME_TICK flg=0x50000014 (has extras) extras: Bundle[{android.intent.extra.ALARM_COUNT=1}] ... ... ... ...ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers) Published single-user content providers (by class): * ContentProviderRecord{429d18a8 u0 com.android.phone/.IccProvider} proc=ProcessRecord{429765d8 858:com.android.phone/1001} singleton=true authority=icc ... ... ... ...ACTIVITY MANAGER SERVICES (dumpsys activity services) User 0 active services: * ServiceRecord{429f8668 u0 com.android.bluetooth/.hid.HidService} app=null created=-1h44m27s317ms started=false connections=0 ... ... ... ...ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities) Stack #0: Task id #28 TaskRecord{43525058 #28 A=com.android.systemui U=0 sz=1} Intent { act=com.android.systemui.recent.action.TOGGLE_RECENTS flg=0x10c00000 cmp=com.android.systemui/.recent.RecentsActivity (has extras) } Hist #0: ActivityRecord{428d1ae8 u0 com.android.systemui/.recent.RecentsActivity t28} Intent { act=com.android.systemui.recent.action.TOGGLE_RECENTS flg=0x10800000 cmp=com.android.systemui/.recent.RecentsActivity bnds=[328,886][656,1176] } ProcessRecord{42968230 695:com.android.systemui/u0a12} ... ... ... ...ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes) Process LRU list (sorted by oom_adj, 28 total, non-act at 3, non-svc at 3): PERS #27: sysF/ /Ptrm: 0 605:system/1000 (fixed) ... ... ... ...

每部分的开头部分,都会显示 如果想要查看哪些内容,需要使用哪些命令
adb命令查看 activity列表
根据上面的介绍,可以得知,我们可以使用如下的脚本
adb shell dumpsys activity activities
运行后,得到有如下结构的日志信息:
Stack #45: type=standard mode=fullscreen
isSleeping=false
mBounds=Rect(0, 0 - 0, 0)

Stack #0: type=home mode=fullscreen
isSleeping=false
mBounds=Rect(0, 0 - 0, 0)

Stack #1: type=recents mode=fullscreen
isSleeping=false
mBounds=Rect(0, 0 - 0, 0)

其中每个Stack开始代表一个App的活动栈,每个活动栈有一个id,如上所示的Stack #45中的45。
android系统中目前定义了两种类型的Stack,系统预定义的静态栈,总共有五种,如下所示:
0 HOME_STACK_ID:Home应用以及recents app所在的栈
1 FULLSCREEN_WORKSPACE_STACK_ID:一般应用所在的栈
2 FREEFORM_WORKSPACE_STACK_ID:类似桌面操作系统
3 DOCKED_STACK_ID:分屏
4 PINNED_STACK_ID:画中画栈
另一种栈,是系统动态生成的,它的id就是大于4
我们来看下Stack块中的内容,我们随机抽取了一个stack进行分析
Stack #4: type=standard mode=fullscreen//Stack信息 isSleeping=false mBounds=Rect(0, 0 - 0, 0) Task id #51//Task信息 mBounds=Rect(0, 0 - 0, 0) mMinWidth=-1 mMinHeight=-1 mLastNonFullscreenBounds=null * TaskRecord{e0bdfdf #51 A=android.tplink.sjj.teststack U=0 StackId=4 sz=2} userId=0 effectiveUid=u0a128 mCallingUid=u0a28 mUserSetupComplete=true mCallingPackage=com.cyanogenmod.trebuchet affinity=android.tplink.sjj.teststack intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=android.tplink.sjj.teststack/.MainActivity}//启动信息,包括启动源和被启动app的MainActivity realActivity=android.tplink.sjj.teststack/.MainActivity autoRemoveRecents=false isPersistable=true numFullscreen=2 activityType=1 rootWasReset=true mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE Activities=[ActivityRecord{4542de6 u0 android.tplink.sjj.teststack/.MainActivity t51}, ActivityRecord{898d92a u0 android.tplink.sjj.teststack/.FirstActivity t51}]//当前栈中Activity记录 askedCompatMode=false inRecents=true isAvailable=true mRootProcess=ProcessRecord{ebe742c 31812:android.tplink.sjj.teststack/u0a128} stackId=4 hasBeenVisible=true mResizeMode=RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION mSupportsPictureInPicture=false isResizeable=true lastActiveTime=256734507 (inactive for 30s) * Hist #1: ActivityRecord{898d92a u0 android.tplink.sjj.teststack/.FirstActivity t51}//栈中Activity调用历史(最近的) packageName=android.tplink.sjj.teststack processName=android.tplink.sjj.teststack launchedFromUid=10128 launchedFromPackage=android.tplink.sjj.teststack userId=0 app=ProcessRecord{ebe742c 31812:android.tplink.sjj.teststack/u0a128} Intent { flg=0x10000000 cmp=android.tplink.sjj.teststack/.FirstActivity } frontOfTask=false task=TaskRecord{e0bdfdf #51 A=android.tplink.sjj.teststack U=0 StackId=4 sz=2} taskAffinity=android.tplink.sjj.teststack realActivity=android.tplink.sjj.teststack/.FirstActivity baseDir=/data/app/android.tplink.sjj.teststack-Cu_ETALVG7u-Plh9vUC5Ug==/base.apk dataDir=/data/user/0/android.tplink.sjj.teststack splitDir=[/data/app/android.tplink.sjj.teststack-Cu_ETALVG7u-Plh9vUC5Ug==/split_lib_dependencies_apk.apk, ......................................... fullscreen=true noDisplay=false immersive=false launchMode=2 frozenBeforeDestroy=false forceNewConfig=false mActivityType=standard waitingVisible=false nowVisible=true lastVisibleTime=-33s65ms resizeMode=RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION mLastReportedMultiWindowMode=false mLastReportedPictureInPictureMode=false * Hist #0: ActivityRecord{4542de6 u0 android.tplink.sjj.teststack/.MainActivity t51}//同上,这是在上个历史之前的 packageName=android.tplink.sjj.teststack processName=android.tplink.sjj.teststack launchedFromUid=10028 launchedFromPackage=com.cyanogenmod.trebuchet userId=0 app=ProcessRecord{ebe742c 31812:android.tplink.sjj.teststack/u0a128} Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=android.tplink.sjj.teststack/.MainActivity bnds=[24,204][192,398] } frontOfTask=true task=TaskRecord{e0bdfdf #51 A=android.tplink.sjj.teststack U=0 StackId=4 sz=2} taskAffinity=android.tplink.sjj.teststack realActivity=android.tplink.sjj.teststack/.MainActivity baseDir=/data/app/android.tplink.sjj.teststack-Cu_ETALVG7u-Plh9vUC5Ug==/base.apk dataDir=/data/user/0/android.tplink.sjj.teststack splitDir=[/data/app/android.tplink.sjj.teststack-Cu_ETALVG7u-Plh9vUC5Ug==/split_lib_dependencies_apk.apk, fullscreen=true noDisplay=false immersive=false launchMode=0 frozenBeforeDestroy=false forceNewConfig=false mActivityType=standard waitingVisible=false nowVisible=false lastVisibleTime=-36s658ms resizeMode=RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION mLastReportedMultiWindowMode=false mLastReportedPictureInPictureMode=false .......................................Running activities (most recent first):// 最近该任务栈中活动的Activity,按最近时间排列 TaskRecord{e0bdfdf #51 A=android.tplink.sjj.teststack U=0 StackId=4 sz=2} Run #1: ActivityRecord{898d92a u0 android.tplink.sjj.teststack/.FirstActivity t51} Run #0: ActivityRecord{4542de6 u0 android.tplink.sjj.teststack/.MainActivity t51}mResumedActivity: ActivityRecord{898d92a u0 android.tplink.sjj.teststack/.FirstActivity t51}//当前页面Activity mLastPausedActivity: ActivityRecord{4542de6 u0 android.tplink.sjj.teststack/.MainActivity t51}//最后一个退出前台的Activity ..........................

以上内容中,我们可以找到如下的关键词,
Running activities (most recent first):

该关键词表示是该Stack中正在运行的activity列表,并且按照时间最近排序,第一个为最顶层的Activity
mResumedActivity
该关键词后表示的就是当前正在运行的Activity
mLastPausedActivity
该关键词后表示的就是最近一个退出前台的Activity
【android|adb 打印activity堆栈】快捷查看所有当前正在运行的Activity
adb shell dumpsys activity activities | sed -En -e ‘/Running activities/,Run #0/p’
同理其他的组件,比如Intent、Service等。

    推荐阅读