LoginSignup
15

More than 5 years have passed since last update.

開いているActivityを一発で表示するコマンド

Last updated at Posted at 2017-07-10

コマンド

command
$ adb shell dumpsys activity | grep mFocusedActivity | cut -d ' ' -f6 | rev | cut -d '.' -f1 | rev

注)追記のコマンドのほうがシンプルです。

解説

Chromeアプリで任意のページを開いている状態で検証

Activityの情報を出力する

command
$ adb shell dumpsys activity
出力
なんかぐわーっていっぱい

mFocusedActivityを含む行のみを取得する

command
$adb shell dumpsys activity | grep mFocusedActivity
出力
  mFocusedActivity: ActivityRecord{be4ff93 u0 com.android.chrome/org.chromium.chrome.browser.ChromeTabbedActivity t240}

半角スペース区切りでの六番目を取得

command
$ adb shell dumpsys activityty | grep mFocusedActivity | cut -d ' ' -f6
出力
com.android.chrome/org.chromium.chrome.browser.ChromeTabbedActivity

ひっくり返す

一番うしろの文字列がほしい為、ひっくり返す

command
$ adb shell dumpsys activity | grep mFocusedActivity | rev
出力
ytivitcAdebbaTemorhC.resworb.emorhc.muimorhc.gro/emorhc.diordna.moc

ピリオド区切りの先頭を取得

command
$ adb shell dumpsys activity | grep mFocusedActivity | cut -d ' ' -f6 | rev | cut -d '.' -f1
出力
ytivitcAdebbaTemorhC

再度ひっくり返す

command
$ adb shell dumpsys activity | grep mFocusedActivity | cut -d ' ' -f6 | rev | cut -d '.' -f1 | rev
出力
ChromeTabbedActivity

補足

一応どのアプリを見てもmFocusedActivityの六番目がActivity場所になっていたから、どのアプリでもAcitivityが取得できるはず・・・

追記

別の方法も教えて貰いました

command
$ adb shell dumpsys activity | grep mFocusedActivity | awk -F ' ' '{print $4}' | awk -F '.' '{print $NF}'

こっちのがシンプルでおしゃれ!

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15