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

  • 查看电池所有相关信息
  • adb shell dumpsys battery
  • 模拟充电状态
  • adb shell dumpsys battery set status 2
  • 模拟非充电状态
  • adb shell dumpsys battery set status 1
  • 设置AC/USB/Wireless模式充电
  • adb shell dumpsys battery set ac/usb/wireless 1   -- 开启ac/usb/wireless充电
  • adb shell dumpsys battery set ac/usb/wireless 0   -- 关闭ac/usb/wireless充电
  • 模拟电池电量
  • adb shell dumpsys battery set level x   -- x表示电量 0-100之间取值
  • 模拟断开充电(Android6.0以上)
  • adb shell dumpsys battery unplug
  • 复位,恢复实际状态
  • adb shell dumpsys battery reset

输入adb logcat 即可查看手机日志

想要提取报错的日志,则输入  adb logcat *:E(此处的E是过滤的级别,只显示错误≥E的)

*过滤项格式* : [:priority] , 标签:日志等级, 默认的日志过滤项是 " *:I " ;

– *V* : Verbose (明细);

– *D* : Debug (调试);

– *I* : Info (信息);

– *W* : Warn (警告);

– *E* : Error (错误);

– *F* : Fatal (严重错误);

– *S* : Silent(Super all output) (最高的优先级, 可能不会记载东西);

清除日志缓存   adb locat -c

关于ADB命令工具的更多相关文章

  1. 【Xamarin挖墙脚系列:Android最重要的命令工具ADB】

    原文:[Xamarin挖墙脚系列:Android最重要的命令工具ADB] adb工具提供了很好的基于命令的对系统的控制. 以前说过,安卓的本质是运行在Linux上的虚机系统.在Linux中,对系统进行 ...

  2. [adb 学习篇] python将adb命令集合到一个工具上

    https://testerhome.com/topics/6938 qzhi的更全面,不过意思是一样的,另外补充一个开源的https://github.com/264768502/adb_wrapp ...

  3. 分享一个常用Adb命令

    分享一个常用Adb命令 首先 首先感谢@xuxu的常用adb命令,收益良多,但是已经不能满足于我,所以补充了下. 再者 好久没发帖了,最近论坛老司机们都在讨论/总结,我就用这个干货回报吧. 最后 基于 ...

  4. Android的常用adb命令

    第一部分:1. ubuntu下配置环境anroid变量:在终端执行 sudo gedit /etc/profile 打开文本编辑器,在最后追加#setandroid environment2. 运行E ...

  5. ADB命令详解

    一.adb介绍 ADB的全称为Android Debug Bridge,字面意思就是安卓调试桥接,简单点说,它是Android系统提供的一套工具,通过它,我们可以在电脑上建立一个连接到手机的通道,然后 ...

  6. python学习之——调用adb命令完成移动端界面测试

    实现原理 Hierarchy Viewer:获得当前手机实时的UI信息,方便用于手机的自动化测试: python中的subprocess.Popen():调用系统命令: uiautomator工具:获 ...

  7. Android测试——adb命令

    Adb (Android Debug Bridge)起到调试桥的作用. 通过adb我们可以在Eclipse中方便通过DDMS来调试Android程序.adb采用监听Socket TCP 5554等端口 ...

  8. 常用的adb命令

    在平时的工作中,会经常用到adb命令,在这里稍微整理了一下. 一.概要 1.什么是adb? adb全称为Android Debug Bridge,就是起到调试桥的作用.顾名思义,adb就是一个debu ...

  9. Android 开发中常用 ADB 命令总结

    adb 的全称为 Android Debug Bridge,就是起到调试桥的作用.通过 adb 我们可以在 Eclipse 中方便通过 DDMS 来调试 Android 程序,说白了就是 debug ...

  10. APK文件安装模拟器和ADB命令的使用

    1.安装APK文件到模拟器 Android手机使用的执行文件为APK格式,类似于Windows平台的exe文件.在Android模拟器中安装APK文件有多种方法,如果你是开发人员,可以通过Eclips ...

随机推荐

  1. 看K线学炒股(8.9)

    今天是食品饮料类题材大涨的一天,相应板块涨了6个多点,看着真是诱人.我以前关注的两只股票:海天味业和三全食品,今天都大涨.三全食品接近涨停.这种票容易选出来但也很难拿住.比如前些天买入了,结果8.5的 ...

  2. 【STM32】SYSCLK配置|学习笔记

    一.RCC原理 所有stm32的外设都是挂载在相应的时钟上的,如下 挂载在AHB上的外设 挂载在APB1上的 APB2上的 所以RCC很重要,特别像TIM这种对时钟特别敏感的外设就必须把SYSCLK确 ...

  3. Centos安装后出现please make your choice from '1' to eter the license information spoke | 'q' to quit |'c' to continue |'r' to refresh

    这是要求用户阅读或者接收协议: 解决方法:输入"1",按Enter键   阅读许可协议,输入"2",按Enter键  接受许可协议,输入"q" ...

  4. std::string实现split和trim方法

    void split(const std::string& str, const std::string& strDelimiter, std::vector<std::stri ...

  5. 1.CD冷却效果

    CD冷却效果.. 一.将需要用到的图片复制到 PS 中做去色处理,将图片保存为 PNG 格式.如下 二.将制作好的图片导入 Unity 中,做成图集 三.在虚拟按键上添加 UI - Image 制作 ...

  6. 手机端 replaceAll没有执行吗

    let item = _styleArr[i].replace(' ','').split(':'): 不知道为什么,好像replaceAll没有执行,后来改成replace好了.

  7. HTML多媒体

    多媒体(一).插入音频.视频和flash在网页中插入音频.视频和flash都是使用embed标签. 语法: <embed src="多媒体文件地址" width=" ...

  8. PLSQL中文乱码问题(显示问号)解决办法

    select userenv('language') from dual; 在环境变量的系统变量中添加配置: NLS_LANG SIMPLIFIED CHINESE_CHINA.ZHS16GBK 这个 ...

  9. C#连接数据库实现开发图书管理系统操作代码

    //客户端登录界面(Form1.cs窗口体系) using System; using System.Collections.Generic; using System.ComponentModel; ...

  10. java面经学习002

    2. Java都有哪些map,分别怎么实现的,具体讲 3. 除了LinkedHashMap,你还知道哪些有序map 4. ConcurrentHashMap讲一讲 5. 为什么要有线程池 6. 线程池 ...