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閱讀 229,362評論 6 537
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,013評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,346評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,421評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,146評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,534評論 1 325
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,585評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,767評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,318評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,074評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,258評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,828評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,486評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,916評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,156評論 1 290
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,993評論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,234評論 2 375

推薦閱讀更多精彩內容

  • 最近看到一些關于adb的相關知識,總結一下,這些常用的命令以后可以直接拿來用~ adb其實就是Android De...
    Mr_馮先生閱讀 4,262評論 0 10
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,692評論 25 708
  • android開發環境中,ADB是我們進行android開發經常要用的調試工具,它的使用當然是我們Android開...
    Memebox閱讀 6,118評論 0 32
  • 簡單的三個字,我愛你。這是這個世界上我唯一覺得,沒有比它來得更真情實意的感受了。 沒有燃枯的燈,沒有遺失的路,沒有...
    Eternal一soso閱讀 326評論 0 1
  • 寫文后,走路至特殊學校。 早飯時,與秀、李虹聽了陽城電臺采訪我的“人生絮語”節目。李虹說我講的不錯。 我所講的,都...
    劉愛國閱讀 228評論 0 2