通過adb shell dumpsys命令擷取當前應用的component

來源:互聯網
上載者:User

標籤:

分類: adb2014-06-22 01:24 837人閱讀 評論(0) 收藏 舉報

androidadb測試monkeyrunnerpython

在android測試中,經常需要知道啟動一個Activity所需要的component,例如在monkeyrunner中啟動一個系統設定:startActivity(component="com.android.settings/com.android.settings.Settings"),那如何擷取該component 呢?

有如下方法:

1.在有root 許可權並且開啟了view server 的前提下,使用sdk/tools目錄下hierarchyviewer.bat 工具可以獲得

2.在sdk/build-tools目錄下有個aapt工具,使用aapt dump badging *.apk可以獲得

3.在CMD視窗中執行adb logcat -v time -s ActivityManager,然後點擊應用進入,如點擊系統設定,進入後會有相應的日誌資訊列印出來,在資訊中尋找 cmp=com.android.settings/.Settings

4.通過adb shell dumpsys命令獲得,這也是我準備主要介紹的方法

在CMD視窗中執行adb shell dumpsys window -h,會顯示下面的協助內容:

C:\Users\xuxu>adb shell dumpsys window -h
Window managerdump options:
[-a] [-h] [cmd]...
cmd may be oneof:
l[astanr]: lastANR information
p[policy]:policy state
a[animator]:animator state
s[essions]:active sessions
d[isplays]:active display contents
 t[okens]:token list
w[indows]:window list
cmd may also bea NAME to dump windows.  NAME may
be a partialsubstring in a window name, a
Window hexobject identifier, or
 "all"for all windows, or
 "visible"for the visible windows.
 -a:include all available server state.

我們使用windows選項,執行adb shell dumpsys window w,在輸出結果中我們可以找到開啟的當前應用的component,而component中總是含有斜杠“/”,所以我們可以使用這個命令得到輸出(進入系統設定應用),adb shell dumpsyswindow w | findstr \/ ,需要轉義斜杠“/”,在linux下需要把findstr換成grep,此時輸出的內容還是會比較多,不容易尋找,再結果分析,發現可以再尋找字串“name=”,

接下來重新執行adb shell dumpsyswindow w | findstr \/ | findstr name= ,會輸出下面的結果:

C:\Users\xuxu>adb shell dumpsyswindow w | findstr \/ | findstr name=
      mSurface=Surface(name=com.android.settings/com.android.settings.Settings)

com.android.settings/com.android.settings.Settings 就是我們需要的component

接下來用python語句來擷取該component:

[python] view plaincopy

  1. import os  
  2. import re  
  3.   
  4. def getFocusedPackageAndActivity():  
  5.   
  6.         pattern = re.compile(r"[a-zA-Z0-9\.]+/[a-zA-Z0-9\.]+")  
  7.         out = os.popen("adb shell dumpsys window windows | findstr \/ | findstr name=").read()  
  8.         list = pattern.findall(out)  
  9.         component = list[0]  
  10. 10.   
  11. 11.         return component  

12. print getFocusedPackageAndActivity()  

 

 

列印結果:com.android.settings/com.android.settings.Settings

如此就可以在使用monkeyrunner中的startActivity方法時調用該方法將擷取到的component傳入參數了!

 

 

附出處:http://blog.csdn.net/gb112211/article/details/33073191

通過adb shell dumpsys命令擷取當前應用的component

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.