通过ADB获取当前活动的名称

问题描述 投票:0回答:15

有没有办法通过 adb 获取当前正在运行的活动的详细信息。

android adb ddms
15个回答
225
投票

我更喜欢解析

dumpsys window windows
的结果而不是
dumpsys activity

adb shell "dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'"

键盘锁或最近任务列表过去不会显示为活动,但您可以使用

mCurrentFocus
查看它们。我已经在这个答案中解释了原因。

编辑备注:

  • grep
    是一个适用于类 Unix 操作系统(例如 Linux、macOS 和 Android)的命令行实用程序。对于 Windows 用户,在
    adb shell
    之后的命令中使用引号非常重要:"" 或 `` 或 ''。使用引号,
    grep
    命令将被传递到
    adb shell
    ,并将在 Android 内部而不是主机操作系统中进行处理。

  • 如果没有引号,

    adb shell dumpsys window windows
    命令将输出所有内容,然后它将通过管道传输到操作系统中的
    grep
    。但有了引号,
    "dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'"
    ,管道过程发生在 Android 内部,这使得它可以在 Windows 中工作。

  • 虽然结果在类 Unix 操作系统中是相同的,但连续多次执行不带引号的命令最终将导致随着时间的推移进程变慢。


119
投票

您可以使用此命令,

adb shell dumpsys activity

您可以在活动堆栈中找到当前活动名称。

输出:-

 Sticky broadcasts:
 * Sticky action android.intent.action.BATTERY_CHANGED:
   Intent: act=android.intent.action.BATTERY_CHANGED flg=0x60000000
     Bundle[{icon-small=17302169, present=true, scale=100, level=50, technology=Li-ion, status=2, voltage=0, plugged=1, health=2, temperature=0}]
 * Sticky action android.net.thrott.THROTTLE_ACTION:
   Intent: act=android.net.thrott.THROTTLE_ACTION
     Bundle[{level=-1}]
 * Sticky action android.intent.action.NETWORK_SET_TIMEZONE:
   Intent: act=android.intent.action.NETWORK_SET_TIMEZONE flg=0x20000000
     Bundle[mParcelledData.dataSize=68]
 * Sticky action android.provider.Telephony.SPN_STRINGS_UPDATED:
   Intent: act=android.provider.Telephony.SPN_STRINGS_UPDATED flg=0x20000000
     Bundle[mParcelledData.dataSize=156]
 * Sticky action android.net.thrott.POLL_ACTION:
   Intent: act=android.net.thrott.POLL_ACTION
     Bundle[{cycleRead=0, cycleStart=1349893800000, cycleEnd=1352572200000, cycleWrite=0}]
 * Sticky action android.intent.action.SIM_STATE_CHANGED:
   Intent: act=android.intent.action.SIM_STATE_CHANGED flg=0x20000000
     Bundle[mParcelledData.dataSize=116]
 * Sticky action android.intent.action.SIG_STR:
   Intent: act=android.intent.action.SIG_STR flg=0x20000000
     Bundle[{EvdoSnr=-1, CdmaDbm=-1, GsmBitErrorRate=-1, CdmaEcio=-1, EvdoDbm=-1, GsmSignalStrength=7, EvdoEcio=-1, isGsm=true}]
 * Sticky action android.intent.action.SERVICE_STATE:
   Intent: act=android.intent.action.SERVICE_STATE flg=0x20000000
     Bundle[{cdmaRoamingIndicator=0, operator-numeric=310260, networkId=0, state=0, emergencyOnly=false, operator-alpha-short=Android, radioTechnology=3, manual=false, cssIndicator=false, operator-alpha-long=Android, systemId=0, roaming=false, cdmaDefaultRoamingIndicator=0}]
 * Sticky action android.net.conn.CONNECTIVITY_CHANGE:
   Intent: act=android.net.conn.CONNECTIVITY_CHANGE flg=0x30000000
     Bundle[{networkInfo=NetworkInfo: type: mobile[UMTS], state: CONNECTED/CONNECTED, reason: simLoaded, extra: internet, roaming: false, failover: false, isAvailable: true, reason=simLoaded, extraInfo=internet}]
 * Sticky action android.intent.action.NETWORK_SET_TIME:
   Intent: act=android.intent.action.NETWORK_SET_TIME flg=0x20000000
     Bundle[mParcelledData.dataSize=36]
 * Sticky action android.media.RINGER_MODE_CHANGED:
   Intent: act=android.media.RINGER_MODE_CHANGED flg=0x70000000
     Bundle[{android.media.EXTRA_RINGER_MODE=2}]
 * Sticky action android.intent.action.ANY_DATA_STATE:
   Intent: act=android.intent.action.ANY_DATA_STATE flg=0x20000000
     Bundle[{state=CONNECTED, apnType=*, iface=/dev/omap_csmi_tty1, apn=internet, reason=simLoaded}]

 Activity stack:
 * TaskRecord{450adb90 #22 A org.chanakyastocktipps.com}
   clearOnBackground=false numActivities=2 rootWasReset=false
   affinity=org.chanakyastocktipps.com
   intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.chanakyastocktipps.com/.ui.SplashScreen}
   realActivity=org.chanakyastocktipps.com/.ui.SplashScreen
   lastActiveTime=15107753 (inactive for 4879s)
   * Hist #2: HistoryRecord{450d7ab0 org.chanakyastocktipps.com/.ui.Profile}
       packageName=org.chanakyastocktipps.com processName=org.chanakyastocktipps.com
       launchedFromUid=10046 app=ProcessRecord{44fa3450 1065:org.chanakyastocktipps.com/10046}
       Intent { cmp=org.chanakyastocktipps.com/.ui.Profile }
       frontOfTask=false task=TaskRecord{450adb90 #22 A org.chanakyastocktipps.com}
       taskAffinity=org.chanakyastocktipps.com
       realActivity=org.chanakyastocktipps.com/.ui.Profile
       base=/data/app/org.chanakyastocktipps.com-1.apk/data/app/org.chanakyastocktipps.com-1.apk data=/data/data/org.chanakyastocktipps.com
       labelRes=0x7f09000b icon=0x7f020065 theme=0x1030007
       stateNotNeeded=false componentSpecified=true isHomeActivity=false
       configuration={ scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=1 layout=18 uiMode=17 seq=3}
       resultTo=HistoryRecord{44f523c0 org.chanakyastocktipps.com/.ui.MainScreen} resultWho=null resultCode=4
       launchFailed=false haveState=false icicle=null
       state=RESUMED stopped=false delayedResume=false finishing=false
       keysPaused=false inHistory=true persistent=false launchMode=0
       fullscreen=true visible=true frozenBeforeDestroy=false thumbnailNeeded=false idle=true
       waitingVisible=false nowVisible=true
   * Hist #1: HistoryRecord{44f523c0 org.chanakyastocktipps.com/.ui.MainScreen}
       packageName=org.chanakyastocktipps.com processName=org.chanakyastocktipps.com
       launchedFromUid=10046 app=ProcessRecord{44fa3450 1065:org.chanakyastocktipps.com/10046}
       Intent { cmp=org.chanakyastocktipps.com/.ui.MainScreen }
       frontOfTask=true task=TaskRecord{450adb90 #22 A org.chanakyastocktipps.com}
       taskAffinity=org.chanakyastocktipps.com
       realActivity=org.chanakyastocktipps.com/.ui.MainScreen
       base=/data/app/org.chanakyastocktipps.com-1.apk/data/app/org.chanakyastocktipps.com-1.apk data=/data/data/org.chanakyastocktipps.com
       labelRes=0x7f09000b icon=0x7f020065 theme=0x1030007
       stateNotNeeded=false componentSpecified=true isHomeActivity=false
       configuration={ scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=1 layout=18 uiMode=17 seq=3}
       launchFailed=false haveState=true icicle=Bundle[mParcelledData.dataSize=1344]
       state=STOPPED stopped=true delayedResume=false finishing=false
       keysPaused=false inHistory=true persistent=false launchMode=0
       fullscreen=true visible=false frozenBeforeDestroy=false thumbnailNeeded=false idle=true
 * TaskRecord{450615a0 #2 A com.android.launcher}
   clearOnBackground=true numActivities=1 rootWasReset=false
   affinity=com.android.launcher
   intent={act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.android.launcher/com.android.launcher2.Launcher}
   realActivity=com.android.launcher/com.android.launcher2.Launcher
   lastActiveTime=12263090 (inactive for 7724s)
   * Hist #0: HistoryRecord{4505d838 com.android.launcher/com.android.launcher2.Launcher}
       packageName=com.android.launcher processName=com.android.launcher
       launchedFromUid=0 app=ProcessRecord{45062558 129:com.android.launcher/10025}
       Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.android.launcher/com.android.launcher2.Launcher }
       frontOfTask=true task=TaskRecord{450615a0 #2 A com.android.launcher}
       taskAffinity=com.android.launcher
       realActivity=com.android.launcher/com.android.launcher2.Launcher
       base=/system/app/Launcher2.apk/system/app/Launcher2.apk data=/data/data/com.android.launcher
       labelRes=0x7f0c0002 icon=0x7f020044 theme=0x7f0d0000
       stateNotNeeded=true componentSpecified=false isHomeActivity=true
       configuration={ scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=1 layout=18 uiMode=17 seq=3}
       launchFailed=false haveState=true icicle=Bundle[mParcelledData.dataSize=3608]
       state=STOPPED stopped=true delayedResume=false finishing=false
       keysPaused=false inHistory=true persistent=false launchMode=2
       fullscreen=true visible=false frozenBeforeDestroy=false thumbnailNeeded=false idle=true

 Running activities (most recent first):
   TaskRecord{450adb90 #22 A org.chanakyastocktipps.com}
     Run #2: HistoryRecord{450d7ab0 org.chanakyastocktipps.com/.ui.Profile}
     Run #1: HistoryRecord{44f523c0 org.chanakyastocktipps.com/.ui.MainScreen}
   TaskRecord{450615a0 #2 A com.android.launcher}
     Run #0: HistoryRecord{4505d838 com.android.launcher/com.android.launcher2.Launcher}

 mPausingActivity: null
 mResumedActivity: HistoryRecord{450d7ab0 org.chanakyastocktipps.com/.ui.Profile}
 mFocusedActivity: HistoryRecord{450d7ab0 org.chanakyastocktipps.com/.ui.Profile}
 mLastPausedActivity: HistoryRecord{44f523c0 org.chanakyastocktipps.com/.ui.MainScreen}

 mCurTask: 22

 Running processes (most recent first):
   App  #13: adj=vis  /F 45052120 119:com.android.inputmethod.latin/10003 (service)
             com.android.inputmethod.latin.LatinIME<=ProcessRecord{44ec2698 59:system/1000}
   PERS #12: adj=sys  /F 44ec2698 59:system/1000 (fixed)
   App  #11: adj=fore /F 44fa3450 1065:org.chanakyastocktipps.com/10046 (top-activity)
   App  #10: adj=bak  /B 44e7c4c0 299:com.svox.pico/10028 (bg-empty)
   App  # 9: adj=bak+1/B 450f7ef0 288:com.dreamreminder.org:feather_system_receiver/10057 (bg-empty)
   App  # 8: adj=bak+2/B 4503cc38 201:com.android.defcontainer/10010 (bg-empty)
   App  # 7: adj=home /B 45062558 129:com.android.launcher/10025 (home)
   App  # 6: adj=bak+3/B 450244d8 276:android.process.media/10002 (bg-empty)
   App  # 5: adj=bak+4/B 44f2b9b8 263:com.android.quicksearchbox/10012 (bg-empty)
   App  # 4: adj=bak+5/B 450beec0 257:com.android.protips/10007 (bg-empty)
   App  # 3: adj=bak+6/B 44ff37b8 270:com.android.music/10022 (bg-empty)
   PERS # 2: adj=core /F 45056818 124:com.android.phone/1001 (fixed)
   App  # 1: adj=bak+7/B 45080c38 238:com.dreamreminder.org/10057 (bg-empty)
   App  # 0: adj=empty/B 4507d030 229:com.android.email/10030 (bg-empty)

 PID mappings:
   PID #59: ProcessRecord{44ec2698 59:system/1000}
   PID #119: ProcessRecord{45052120 119:com.android.inputmethod.latin/10003}
   PID #124: ProcessRecord{45056818 124:com.android.phone/1001}
   PID #129: ProcessRecord{45062558 129:com.android.launcher/10025}
   PID #201: ProcessRecord{4503cc38 201:com.android.defcontainer/10010}
   PID #229: ProcessRecord{4507d030 229:com.android.email/10030}
   PID #238: ProcessRecord{45080c38 238:com.dreamreminder.org/10057}
   PID #257: ProcessRecord{450beec0 257:com.android.protips/10007}
   PID #263: ProcessRecord{44f2b9b8 263:com.android.quicksearchbox/10012}
   PID #270: ProcessRecord{44ff37b8 270:com.android.music/10022}
   PID #276: ProcessRecord{450244d8 276:android.process.media/10002}
   PID #288: ProcessRecord{450f7ef0 288:com.dreamreminder.org:feather_system_receiver/10057}
   PID #299: ProcessRecord{44e7c4c0 299:com.svox.pico/10028}
   PID #1065: ProcessRecord{44fa3450 1065:org.chanakyastocktipps.com/10046}

 mHomeProcess: ProcessRecord{45062558 129:com.android.launcher/10025}
 mConfiguration: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=1 layout=18 uiMode=17 seq=3}
 mConfigWillChange: false
 mSleeping=false mShuttingDown=false

90
投票

Android Q 为我打破了其中大部分。这是一个似乎可以工作的新功能(至少在 Android Q 上)。

adb shell "dumpsys activity activities | grep ResumedActivity"

输出看起来像:

mResumedActivity: ActivityRecord{7f6df99 u0 com.sample.app/.feature.SampleActivity t92}

编辑:对我来说也适用于 Android R


29
投票

dumpsys window windows
提供有关当前活动的更多详细信息:

adb shell "dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'"
  mCurrentFocus=Window{41d2c970 u0 com.android.launcher/com.android.launcher2.Launcher}
  mFocusedApp=AppWindowToken{4203c170 token=Token{41b77280 ActivityRecord{41b77a28 u0 com.android.launcher/com.android.launcher2.Launcher t3}}}

但是,为了找到进程 ID(例如,终止当前活动),请使用

dumpsys activity
,并在“top-activity”上使用 grep:

adb shell "dumpsys activity | grep top-activity"
    Proc # 0: fore  F/A/T  trm: 0 3074:com.android.launcher/u0a8 (top-activity)

adb shell "kill 3074"

17
投票

如果您只想过滤掉应用程序当前正在运行/暂停的活动,您可以使用以下命令:

adb shell dumpsys activity activities | grep 'Hist #' | grep 'YOUR_PACKAGE_NAME'

例如:

adb shell dumpsys activity activities | grep 'Hist #' | grep 'com.supercell.clashroyale'

输出将类似于:

* Hist #2: ActivityRecord{26ba44b u10 com.supercell.clashroyale/StartActivity t27770}
* Hist #1: ActivityRecord{2f3a0236 u10 com.supercell.clashroyale/SomeActivity t27770}
* Hist #0: ActivityRecord{20bbb4ae u10 com.supercell.clashroyale/OtherActivity t27770}

请注意,输出显示了实际的活动堆栈,即最顶层的活动是当前正在显示的活动。


16
投票

旧答案在新的 Android 版本中停止工作。现在我使用以下内容:

adb shell "dumpsys activity activities | grep ResumedActivity"

16
投票

这对我有用:

adb shell dumpsys activity

这显示当前活动名称:

adb shell dumpsys activity activities | grep mFocusedActivity | cut -d . -f 5 | cut -d ' ' -f 1

有关获取活动名称的更准确脚本,请参阅this


8
投票

你可以尝试这个命令,

adb shell dumpsys activity recents

您可以在活动堆栈中找到当前活动名称。

获取最近的活动名称:

adb shell dumpsys activity recents | find "Recent #0"

7
投票

adb shell dumpsys activity activities | grep mFocusedApp
对我有用。

adb shell "dumpsys activity activities | grep mResumedActivity"
不适用于我的设备:
Google Pixel 4 Android 12, API 32

adb shell "dumpsys activity activities | grep -E 'mCurrentFocus|mFocusedApp'"

对我也有用。


5
投票

在Windows中,这个命令可以让我显示当前的活动名称

adb shell dumpsys window windows | find "mCurrentFocus"

输出:

mCurrentFocus=Window{a43a55b u0 com.android.contacts/com.android.contacts.activities.TwelveKeyDialer}

使用“grep”命令的其他解决方案在我的 Windows 电脑中产生错误

'grep' is not recognized as an internal or external command, operable program or batch file.

因此,使用“查找”解决了我的情况下的错误。


4
投票

以下命令可用于查找包和当前活动名称。发现这个非常有用的命令可以快速获取有关应用程序的这两个信息,特别是在使用 Appium 开发测试时。

adb shell dumpsys window windows | grep -E 'mCurrentFocus'

此命令的响应包含包名称和当前 Activity。例如:下面的“com.android.contacts”是包,“com.android.contacts.activities.TwelveKeyDialer”是通过adb连接的手机上启动的当前Activity。

mCurrentFocus=Window{2089af8 u0 com.android.contacts/com.android.contacts.activities.TwelveKeyDialer}

参考:http://www.automationtestinghub.com/apppackage-and-appactivity-name/


3
投票

这是一个比命令行 adb 解决方案(确实有效)更简单的解决方案。它更容易,因为它更加图形化并且可以在 Eclipse 中完成。

在连接了 Android 设备的 Eclipse 中,转到“窗口”菜单 >“显示视图”>“其他...”,使用过滤器文本“Windows”拉出 Android > Windows...显示此内容。列表中的顶部项目是当前活动。

另一种方法是显示“层次结构视图”透视图,默认情况下将显示 Windows 窗口。


2
投票

当前活动堆栈

adb shell dumpsys activity top | grep "ACTIVITY"

结果:

ACTIVITY com.android.vending/.AssetBrowserActivity 637831e pid=1795
ACTIVITY com.google.android.apps.nexuslauncher/.NexusLauncherActivity a2ca51d pid=1053
ACTIVITY com.google.android.gm/.ConversationListActivityGmail 621126d pid=28250

前台活动,当前可见

adb shell dumpsys activity top | grep "ACTIVITY" | tail -n 1

结果:

ACTIVITY com.google.android.gm/.ConversationListActivityGmail 621126d pid=28250

仅前台活动名称

adb shell dumpsys activity top | grep "ACTIVITY" | tail -n 1 | sed -E 's/.*\/([^ ]+).*/\1/'

结果:

.ConversationListActivityGmail

仅前台应用程序名称

adb shell dumpsys activity top | grep "ACTIVITY" | tail -n 1 | awk '{print $2}' | cut -d '/' -f1

结果:

com.google.android.gm

1
投票

我的版本适用于 Linux 和 Windows:

adb shell "dumpsys activity activities | grep mResumedActivity | cut -d "{" -f2 | cut -d ' ' -f3 | cut -d "/" -f1" 

1
投票

转储当前活动适用于Android 12 / 13

adb shell dumpsys activity | awk -F '[ /}]' '/mCurrentFocus/{print $9}'

格列普

adb shell dumpsys activity | grep -oP 'mCurrentFocus=\S+ \S+ \K\S+(?=})'
© www.soinside.com 2019 - 2024. All rights reserved.