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是和输入法相关的,可以通过它来启用/设置输入法,也可以列出手机中已有的输入法. 

Shell代码   收藏代码
  1. $ adb shell ime list -s  
  2. com.sohu.inputmethod.sogou/.SogouIME  
  3. com.google.android.inputmethod.pinyin/.PinyinIME  
  4. com.sohu.inputmethod.sogou.xiaomi/.SogouIME  

 

input

input命令可以模拟输入

比如我们想在输入框内输入123

Shell代码   收藏代码
  1. adb shell input text 123  

注意,要满足几点,首先要聚焦在输入框,另外最好使用原生输入法

 

我们也可以模拟系统按键,比如返回键

Shell代码   收藏代码
  1. adb shell input keyevent KEYCODE_BACK  

我们也可以模拟点击事件,比如点击x=900,y=150

Shell代码   收藏代码
  1. $ adb shell input tap 900 150  

 

wm

wm命令可以获得分辨率/屏幕密度等

Shell代码   收藏代码
  1. $ adb shell wm size  
  2. Physical size: 1080x1920  
  3. $ adb shell wm density  
  4. Physical density: 480  

还可以修改显示输入图像的比例,不过不知道有什么用,大家可以试试这个命令.

Shell代码   收藏代码
  1. $ wm overscan 10,10,100,100  
  2. $ wm overscan reset  

试过之后记得执行reset

 

screencap

screencap用来截屏

Shell代码   收藏代码
  1. $ adb shell screencap -h  
  2. usage: screencap [-hp] [-d display-id] [FILENAME]  
  3.    -h: this message  
  4.    -p: save the file as a png.  
  5.    -d: specify the display id to capture, default 0.  
  6. If FILENAME ends with .png it will be saved as a png.  
  7. If FILENAME is not given, the results will be printed to stdout.  

我们可以直接将截屏保存到电脑中

Shell代码   收藏代码
  1. $ adb shell screencap -p | sed 's/\r$//' > screen.png  

也可以将截图保存到sd卡中

Shell代码   收藏代码
  1. $ adb shell 'cd sdcard; screencap -p screen.png'  
  2. $ adb shell ls -l sdcard/screen.png  
  3. -rw-rw---- root     sdcard_rw   197116 2016-06-21 11:49 screen.png  

 

uiautomator

uiautomator可以用来做UI测试,也可以dump出当前UI结构.如果觉得hierarchy不好用,也可以试试这个命令.只不过结果是xml形式,信息也很全.

Shell代码   收藏代码
  1. $ adb shell uiautomator   
  2. Usage: uiautomator <subcommand> [options]  
  3.   
  4. Available subcommands:  
  5.   
  6. help: displays help message  
  7.   
  8. runtest: executes UI automation tests  
  9.     runtest <class spec> [options]  
  10.     <class spec>: <JARS> < -c <CLASSES> | -e class <CLASSES> >  
  11.       <JARS>: a list of jar files containing test classes and dependencies. If  
  12.         the path is relative, it's assumed to be under /data/local/tmp. Use  
  13.         absolute path if the file is elsewhere. Multiple files can be  
  14.         specified, separated by space.  
  15.       <CLASSES>: a list of test class names to run, separated by comma. To  
  16.         a single method, use TestClass#testMethod format. The -e or -c option  
  17.         may be repeated. This option is not required and if not provided then  
  18.         all the tests in provided jars will be run automatically.  
  19.     options:  
  20.       --nohup: trap SIG_HUP, so test won't terminate even if parent process  
  21.                is terminated, e.g. USB is disconnected.  
  22.       -e debug [true|false]: wait for debugger to connect before starting.  
  23.       -e runner [CLASS]: use specified test runner class instead. If  
  24.         unspecified, framework default runner will be used.  
  25.       -e <NAME> <VALUE>: other name-value pairs to be passed to test classes.  
  26.         May be repeated.  
  27.       -e outputFormat simple | -s: enabled less verbose JUnit style output.  
  28.   
  29. dump: creates an XML dump of current UI hierarchy  
  30.     dump [--verbose][file]  
  31.       [--compressed]: dumps compressed layout information.  
  32.       [file]: the location where the dumped XML should be stored, default is  
  33.       /sdcard/window_dump.xml  
  34.   
  35. events: prints out accessibility events until terminated  

dump当前UI结构

Shell代码   收藏代码
  1. adb shell uiautomator dump sdcard/test.xml  

 

settings

settings可以修改/获取系统设置信息

Shell代码   收藏代码
  1. $ adb shell settings   
  2. usage:  settings [--user NUM] get namespace key  
  3.         settings [--user NUM] put namespace key value  
  4.         settings [--user NUM] delete namespace key  
  5.         settings [--user NUM] list namespace  
  6.   
  7. 'namespace' is one of {system, secure, global}, case-insensitive  
  8. If '--user NUM' is not given, the operations are performed on the owner user.  

比如我们想查看android_id

Shell代码   收藏代码
  1. $ adb shell settings get secure android_id  
  2. 1dbbe170f8995d89  

查看wifi状态

Shell代码   收藏代码
  1. $ adb shell settings get global wifi_on  
  2. 1  

查看日期是否是24小时制

Shell代码   收藏代码
  1. $ adb shell settings get system time_12_24  
  2. 24  

 

svc

svc下面有一组命令,power, data, wifi, usb, nfc,可以控制其开关

例如:

Shell代码   收藏代码
  1. $ svc wifi  
  2. svc wifi  
  3. Control the Wi-Fi manager  
  4.    
  5. usage: svc wifi [enable|disable]  
  6.          Turn Wi-Fi on or off.  

控制移动网络数据开关

Shell代码   收藏代码
  1. $ adb shell svc data disable  
  2. $ adb shell svc data enable  

 

appops

appops可以查看/修改权限相关信息

Shell代码   收藏代码
  1. $ adb shell appops get com.android.phone   
  2. VIBRATE: allow; time=+1d3h57m1s111ms ago; duration=+63ms  
  3. READ_CONTACTS: allow; time=+2h10m59s285ms ago  
  4. READ_SMS: allow; time=+2h10m49s858ms ago  
  5. WRITE_SMS: allow; time=+3m46s339ms ago  
  6. READ_ICC_SMS: allow; time=+2h10m49s859ms ago  
  7. WRITE_CLIPBOARD: allow; time=+10d2h24m17s819ms ago  
  8. WAKE_LOCK: allow; time=+5s122ms ago; duration=+14ms  
  9. READ_EXTERNAL_STORAGE: allow; time=+14h31m4s898ms ago  
  10. WRITE_EXTERNAL_STORAGE: allow; time=+14h31m4s898ms ago  
  11.   
  12. $ adb shell appops get com.android.phone READ_CONTACTS  
  13. READ_CONTACTS: allow; time=+2h28m33s274ms ago  

 

am和pm这两个命令应该算是最复杂也是最常用的了.我们可以通过am启动页面,发送广播等,可以通过pm列出手机中的app,启用禁用app等.当然有一些是需要root权限的.这里就不再介绍了.

android手机中的命令行工具不只这些,在frameworks/native/cmds中也有一些命令,比如我们常用的dumpsys,在我之前的blog中也介绍过.

 

了解了这些工具,我们就可以写一些脚本来获取手机和app信息, 省去了log也方便查看和调试. 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值