Android adb / adb shell命令

adb全名Andorid Debug Bridge. 直接翻译安卓调试桥,环境变量中配置好adb的环境变量,直接通过命令行可以省很多事。
配置好环境变量直接 win+X(win10)快捷键打开命令提示符,输入adb 或者 adb help 打印所有提示信息,如图。

adb help
 Android Debug Bridge version 1.0.26

 -d                            - directs command to the only connected USB device
                             returns an error if more than one USB device is present.
directs command to the only connected USB device
                             returns an error if more than one USB device is present.
 -e                            - directs command to the only running emulator.
                             returns an error if more than one emulator is running.
 -s <serial number>            - directs command to the USB device or emulator with
                             the given serial number. Overrides ANDROID_SERIAL
                             environment variable.
 -p <product name or path>     - simple product name like 'sooner', or
                             a relative/absolute path to a product
                             out directory like 'out/target/product/sooner'.
                             If -p is not specified, the ANDROID_PRODUCT_OUT
                             environment variable is used, which must
                             be an absolute path.
 devices                       - list all connected devices
 connect <host>:<port>         - connect to a device via TCP/IP
 disconnect <host>:<port>      - disconnect from a TCP/IP device

device commands:
  adb push <local> <remote>    - copy file/dir to device
  adb pull <remote> [<local>]  - copy file/dir from device
  adb sync [ <directory> ]     - copy host->device only if changed
                             (see 'adb help all')
  adb shell                    - run remote shell interactively
  adb shell <command>          - run remote shell command
  adb emu <command>            - run emulator console command
  adb logcat [ <filter-spec> ] - View device log
  adb forward <local> <remote> - forward socket connections
                             forward specs are one of:
                               tcp:<port>
                               localabstract:<unix domain socket name>
                               localreserved:<unix domain socket name>
                               localfilesystem:<unix domain socket name>
                               dev:<character device name>
                               jdwp:<process pid> (remote only)
  adb jdwp                     - list PIDs of processes hosting a JDWP transport
  adb install [-l] [-r] [-s] <file> - push this package file to the device and install it
                             ('-l' means forward-lock the app)
                             ('-r' means reinstall the app, keeping its data)
                             ('-s' means install on SD card instead of internal storage)
  adb uninstall [-k] <package> - remove this app package from the device
                             ('-k' means keep the data and cache directories)
  adb bugreport                - return all information from the device
                             that should be included in a bug report.

  adb help                     - show this help message
  adb version                  - show version num

DATAOPTS:
 (no option)                   - don't touch the data partition
  -w                           - wipe the data partition
  -d                           - flash the data partition

scripting:
  adb wait-for-device          - block until device is online
  adb start-server             - ensure that there is a server running
  adb kill-server              - kill the server if it is running
  adb get-state                - prints: offline | bootloader | device
  adb get-serialno             - prints: <serial-number>
  adb status-window            - continuously print device status for a specified device
  adb remount                  - remounts the /system partition on the device read-write
  adb reboot [bootloader|recovery] - reboots the device, optionally into the bootloader or recovery program
  adb reboot-bootloader        - reboots the device into the bootloader
  adb root                     - restarts the adbd daemon with root permissions
  adb usb                      - restarts the adbd daemon listening on USB
  adb tcpip <port>             - restarts the adbd daemon listening on TCP on the specified port
networking:
  adb ppp <tty> [parameters]   - Run PPP over USB.
 Note: you should not automatically start a PPP connection.
 <tty> refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1
 [parameters] - Eg. defaultroute debug dump local notty usepeerdns

adb sync notes: adb sync [ <directory> ]
  <localdir> can be interpreted in several ways:

  - If <directory> is not specified, both /system and /data partitions will be updated.

  - If it is "system" or "data", only the corresponding partition is updated.

adb shell 命令
adb 命令和 adb shell 命令 。
简单点讲,adb 命令是 adb 这个程序自带的一些命令,而 adb shell 进入目标设备的Linux Shell环境, 在该环境中可以执行一些Linux命令.

pm list package

列出安装在设备上的应用 不带任何选项:列出所有的应用的包名
adb shell pm list package

-s:列出系统应用
adb shell pm list package -s

-3:列出第三方应用
adb shell pm list package -3

-f:列出应用包名及对应的apk名及存放位置
adb shell pm list package -f

-i:列出应用包名及其安装来源,结果显示例子:
adb shell pm list package -i
package:com.zhihu.android installer=com.xiaomi.market

install 安装命令 pm install , 安装应用

目标 apk 存放于 PC 端,请用 adb install 安装(adb install apk路径)

目标 apk 存放于 Android 设备上,请用 pm install 安装

pm uninstall , 卸载应用,同 adb uninstall , 后面跟的参数都是应用的包名

pm clear , 清除应用数据

pm set-install-location , pm get-install-location , 设置应用安装位置,获取应用安装位置

am

am start , 启动一个 Activity,已启动系统相机应用为例

启动相机
adb shell am start -n com.android.camera/.Camera
Starting: Intent { cmp=com.android.camera/.Camera }

先停止目标应用,再启动
adb shell am start -S com.android.camera/.Camera
Stopping: com.android.camera
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.android.camera/.Camera }

等待应用完成启动
adb shell am start -W com.android.camera/.Camera
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.android.camera/.Camera }
Status: ok
Activity: com.android.camera/.Camera
ThisTime: 500
TotalTime: 500
Complete

启动默认浏览器打开一个网页
adb shell am start -a android.intent.action.VIEW -d http://baidu.com
Starting: Intent { act=android.intent.action.VIEW dat=http://baidu.com }

启动拨号器拨打 10086
adb shell am start -a android.intent.action.CALL -d tel:10086
Starting: Intent { act=android.intent.action.CALL dat=tel:xxxxx }

am force-stop , 后跟包名,结束应用

input

这个命令可以向 Android 设备发送按键事件,其源码 Input.java

  • input text , 发送文本内容,不能发送中文

    adb shell input text test123456
    

    前提先将键盘设置为英文键盘

  • input keyevent , 发送按键事件,KeyEvent.java

    adb shell input keyevent KEYCODE_HOME
    

    模拟按下 Home 键 ,源码里面有定义:

    public static final int KEYCODE_HOME = 3;

    因此可以将命令中的 KEYCODE_HOME 替换为 3

  • input tap , 对屏幕发送一个触摸事件

    adb shell input tap 500 500
    

    点击屏幕上坐标为 500 500 的位置

  • input swipe , 滑动事件

    adb shell input swipe 900 500 100 500
    

    从右往左滑动屏幕

    如果版本不低于 4.4 , 可以模拟长按事件

    adb shell input swipe 500 500 501 501 2000
    

    其实就是在小的距离内,在较长的持续时间内进行滑动,最后表现出来的结果就是长按动作

screencap截图命令

adb shell screencap -p /sdcard/screen.png
截屏,保存至 sdcard 目录

screenrecord4.4 新增的录制命令

adb shell screenrecord sdcard/record.mp4
执行命令后操作手机,ctrl + c 结束录制,录制结果保存至 sdcard

ime输入法,Ime.java

列出设备上的输入法

 adb shell ime list -s                           
com.google.android.inputmethod.pinyin/.PinyinIME
com.baidu.input_mi/.ImeService

选择输入法

adb shell ime set com.baidu.input_mi/.ImeService
Input method com.baidu.input_mi/.ImeService selected    

wm Wm.java 获取设备分辨率

adb shell wm size
Physical size: 1080x1920  

getprop

查看 Android 设备的参数信息,只运行 adb shell getprop,结果以 key : value 键值对的形式显示,如要获取某个 key 的值:

获取设备的 sdk 版本
adb shell getprop ro.build.version.sdk

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容

  • 最近看到一些关于adb的相关知识,总结一下,这些常用的命令以后可以直接拿来用~ adb其实就是Android De...
    Mr_冯先生阅读 4,121评论 0 10
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,569评论 25 707
  • android开发环境中,ADB是我们进行android开发经常要用的调试工具,它的使用当然是我们Android开...
    Memebox阅读 5,947评论 0 32
  • 简单的三个字,我爱你。这是这个世界上我唯一觉得,没有比它来得更真情实意的感受了。 没有燃枯的灯,没有遗失的路,没有...
    Eternal一soso阅读 293评论 0 1
  • 写文后,走路至特殊学校。 早饭时,与秀、李虹听了阳城电台采访我的“人生絮语”节目。李虹说我讲的不错。 我所讲的,都...
    刘爱国阅读 214评论 0 2