1. 程式人生 > >android系統提供的常用命令列工具

android系統提供的常用命令列工具

本部落格只要沒有註明“轉”,那麼均為原創,轉貼請註明本部落格連結連結

android提供了不少命令列工具,方便我們除錯和檢視資訊.下面是frameworks/base/cmds(android 6.0.1)中的命令.

$ tree cmds -L 1

cmds

├── am

├── appops

├── app_process

├── appwidget

├── backup

├── bmgr

├── bootanimation

├── bu

├── content

├── dpm

├── hid

├── idmap

├── ime

├── input

├── interrupter

├── media

├── pm

├── requestsync

├── screencap

├── settings

├── sm

├── svc

├── telecom

├── uiautomator

└── wm

上面每一個目錄都是一個/一組命令.其中svc中包括power, data, wifi, usb, nfc這些開關.

這裡只列舉一些我平時可能用的到的命令(am, appops, ime, input, pm, screencap, settings, svc, uiautomator, wm)來演示.先從簡單的開始.

ime

ime是和輸入法相關的,可以通過它來啟用/設定輸入法,也可以列出手機中已有的輸入法.

$ adb shell ime list -s
com.sohu.inputmethod.sogou/.SogouIME
com.google.android.inputmethod.pinyin/.PinyinIME
com.sohu.inputmethod.sogou.xiaomi/.SogouIME

input

input命令可以模擬輸入

比如我們想在輸入框內輸入123 

adb shell input text 123

注意,要滿足幾點,首先要聚焦在輸入框,另外最好使用原生輸入法

我們也可以模擬系統按鍵,比如返回鍵

adb shell input keyevent KEYCODE_BACK

我們也可以模擬點選事件,比如點選x=900,y=150 

$ adb shell input tap 900 150

wm

wm命令可以獲得解析度/螢幕密度等

$ adb shell wm size
Physical size: 1080x1920
$ adb shell wm density
Physical density: 480

還可以修改顯示輸入影象的比例,不過不知道有什麼用,大家可以試試這個命令.

$ wm overscan 10,10,100,100
$ wm overscan reset

試過之後記得執行reset

screencap

screencap用來截圖

$ adb shell screencap -h
usage: screencap [-hp] [-d display-id] [FILENAME]
   -h: this message
   -p: save the file as a png.
   -d: specify the display id to capture, default 0.
If FILENAME ends with .png it will be saved as a png.
If FILENAME is not given, the results will be printed to stdout.

我們可以直接將截圖儲存到電腦中

$ adb shell screencap -p | sed 's/\r$//' > screen.png

也可以將截圖儲存到sd卡中

$ adb shell 'cd sdcard; screencap -p screen.png'
$ adb shell ls -l sdcard/screen.png
-rw-rw---- root     sdcard_rw   197116 2016-06-21 11:49 screen.png

uiautomator

uiautomator可以用來做UI測試,也可以dump出當前UI結構.如果覺得hierarchy不好用,也可以試試這個命令.只不過結果是xml形式,資訊也很全.

$ adb shell uiautomator 
Usage: uiautomator <subcommand> [options]

Available subcommands:

help: displays help message

runtest: executes UI automation tests
    runtest <class spec> [options]
    <class spec>: <JARS> < -c <CLASSES> | -e class <CLASSES> >
      <JARS>: a list of jar files containing test classes and dependencies. If
        the path is relative, it's assumed to be under /data/local/tmp. Use
        absolute path if the file is elsewhere. Multiple files can be
        specified, separated by space.
      <CLASSES>: a list of test class names to run, separated by comma. To
        a single method, use TestClass#testMethod format. The -e or -c option
        may be repeated. This option is not required and if not provided then
        all the tests in provided jars will be run automatically.
    options:
      --nohup: trap SIG_HUP, so test won't terminate even if parent process
               is terminated, e.g. USB is disconnected.
      -e debug [true|false]: wait for debugger to connect before starting.
      -e runner [CLASS]: use specified test runner class instead. If
        unspecified, framework default runner will be used.
      -e <NAME> <VALUE>: other name-value pairs to be passed to test classes.
        May be repeated.
      -e outputFormat simple | -s: enabled less verbose JUnit style output.

dump: creates an XML dump of current UI hierarchy
    dump [--verbose][file]
      [--compressed]: dumps compressed layout information.
      [file]: the location where the dumped XML should be stored, default is
      /sdcard/window_dump.xml

events: prints out accessibility events until terminated

dump當前UI結構

adb shell uiautomator dump sdcard/test.xml

settings

settings可以修改/獲取系統設定資訊

$ adb shell settings 
usage:  settings [--user NUM] get namespace key
        settings [--user NUM] put namespace key value
        settings [--user NUM] delete namespace key
        settings [--user NUM] list namespace

'namespace' is one of {system, secure, global}, case-insensitive
If '--user NUM' is not given, the operations are performed on the owner user.

比如我們想檢視android_id

$ adb shell settings get secure android_id
1dbbe170f8995d89

檢視wifi狀態

$ adb shell settings get global wifi_on
1

檢視日期是否是24小時制

$ adb shell settings get system time_12_24
24

svc

svc下面有一組命令,power, data, wifi, usb, nfc,可以控制其開關

例如:

$ svc wifi
svc wifi
Control the Wi-Fi manager
 
usage: svc wifi [enable|disable]
         Turn Wi-Fi on or off.

控制行動網路資料開關

$ adb shell svc data disable
$ adb shell svc data enable

appops

appops可以檢視/修改許可權相關資訊

$ adb shell appops get com.android.phone 
VIBRATE: allow; time=+1d3h57m1s111ms ago; duration=+63ms
READ_CONTACTS: allow; time=+2h10m59s285ms ago
READ_SMS: allow; time=+2h10m49s858ms ago
WRITE_SMS: allow; time=+3m46s339ms ago
READ_ICC_SMS: allow; time=+2h10m49s859ms ago
WRITE_CLIPBOARD: allow; time=+10d2h24m17s819ms ago
WAKE_LOCK: allow; time=+5s122ms ago; duration=+14ms
READ_EXTERNAL_STORAGE: allow; time=+14h31m4s898ms ago
WRITE_EXTERNAL_STORAGE: allow; time=+14h31m4s898ms ago

$ adb shell appops get com.android.phone READ_CONTACTS
READ_CONTACTS: allow; time=+2h28m33s274ms ago

am和pm這兩個命令應該算是最複雜也是最常用的了.我們可以通過am啟動頁面,傳送廣播等,可以通過pm列出手機中的app,啟用禁用app等.當然有一些是需要root許可權的.這裡就不再介紹了.

android手機中的命令列工具不只這些,在frameworks/native/cmds中也有一些命令,比如我們常用的dumpsys,在我之前的blog中也介紹過.

瞭解了這些工具,我們就可以寫一些指令碼來獲取手機和app資訊, 省去了log也方便檢視和除錯. 

轉貼請保留以下連結

本人blog地址