TShopping

 找回密碼
 註冊
搜索
查看: 2011|回復: 0
打印 上一主題 下一主題

[教學] Android adb tool 功能整理

[複製鏈接]
跳轉到指定樓層
1#
發表於 2013-10-21 18:28:53 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
Adb 全名是 Android Debug Bridge,是開發或使用 Android 時很常用到的工具。使用者可以從Android 官方站下載 SDK,在其中的 platform-tools (原本在 \Tools) 中找到。

當機器上有打開 USB debug mode 時,使用者即可通過adb 進行各種 debug 、底層(linux user space)的 Android 功能。比較常用的功能:

- tools\ddms.bat: Android AP/Framework 層最主要的 debug tool
- 安裝 Android 應用程式
- 連接機器,使用 linux userspace 的功能。 ex: ping, ssh, ftp ... blah blah.
adb的工作方式比較特殊採用監聽Socket TCP 5554等端口的方式讓IDE和Qemu通訊,預設情況下adb會daemon相關的port。


這篇文章主要是整理了一些adb 的基本功能,後面補上一些開發時常用的功能。

文件參考:
- Source code: system/core/adb/ ,除了 adb client 跟 adbd (Android 系統內負責處理 adb 功能的 daemon)的實作外,裡面包含了 serviceoverview 的文件。

功能介紹
1. 通過adb 進入機器或模擬器的shell模式
adb shell
也可以執行各種Linux的命令,其命令格式為:adb shell command
PS: 當 adb shell 之後提示字元為"#"時,表示使用者為 root (最大權限),若是 "$" 則是以shell 權限工作


adb shell ls 就是列出目錄
adb shell dmesg 會列印出Linux kernel log
adb shell cat /proc/kmsg 持續印出 kernel log (需要 root)
adb shell keyevent 1 輸入 keyevent,可輸入的內容參考 adb shell keyevent



2. 安裝Android 應用程式(*.apk)

可執行adb install android123.apk,這樣名為android123的安裝包就會安裝到Android模擬器中,前提是android123.apk文件需要放到SDK\Tools目錄下。
比較特殊的安裝方法還有
"-r": 當已經安裝過舊版本的程式時,可以使用 -r 去覆蓋。
"-f": 強制安裝,通常在安裝程式時會遇到相容問題,可使用此參數解決


3. PC 端與Android 機器的檔案傳輸
除了使用記憶卡模式外,還可使用下面命令可以進行檔案傳輸:
把android123.txt 傳到機器上的/tmp/ 資料夾中:
adb push android123.txt /tmp/android123.txt

從機器上把 android123.txt 抓到PC端:

adb pull /tmp/android123.txt android123.txt

4. 顯示系統資訊 - dumpsys
除了直接輸入 adb shell dumpsys 外,也可以另外指定要顯示的 service,簡列一些參數,用法如:
adb shell dumpsys SurfaceFlinger

battery: 列出基本的電池資訊
batteryinfo: 各種功能使用 power 的狀況,同About Phone 裡面的電池使用狀況。
SurfaceFlinger: 系統的 Surface 使用情況
power: 列出 Power Manager 的參數,如 wakelock 時間等
alarm: 列出目前有註冊 alarm 者

5. 其他
- Android 預設可編譯成三種模式: eng, userdebug, user。一般使用者拿到的機器多是 user 版,當然如果是開發人員,可能會使用 eng 或 userdebug 版 進行debug。或是使用者自行 root 機器後,可使用下列指令取得 root 權限
adb root
- 一般為了防止系統出問題,所以 /system 通常在掛載時會設定為唯讀(read only),當使用者有root權限時,可使用下面指令將系統重新掛載成 R/W 模式,可對 /system 內的檔案做修改
adb remount
- 如果在使用 adb 時發現有* daemon not running. starting it now *的提示可以結束adb
adb kill-server

- 顯示 android 機器連接狀況

adb devices
結果如下
List of devices attached
1234567890ABCDEF        device


- 等待正在運行的設備
adb wait-for-device

- Port forwarding,在某些應用如模擬器的網路連接使用、VNC時,會用到這項功能。主要是用來將機器上的的TCP port 5555 轉發到 port 1234

adb forward tcp:5555 tcp:1234

- 擷取系統內的各種資訊,產生 bug report

adb bugreport






Android - How to mount the SDCard image file to Android Emulator

(1)首先必須產生SDCard的image file
mksdcard: create a blank FAT32 image to be used with the Android emulator
usage: mksdcard [-l label]
if is a simple integer, it specifies a size in bytes
if is an integer followed by 'K', it specifies a size in KiB
if is an integer followed by 'M', it specifies a size in MiB

ex:mksdcard 1024M sdcard.iso

(2)讓SDCard連到Android Emulator
a:./emulator -sdcard ~/.android/SDK-1.0/sdcard.iso
b:用Eclipse中設定程式的Open Run Dialog裏,Target頁籤的Aditional Emulator Command Line Option中加入啟動參數 -sdcard scard.iso

(3)透過adb傳收檔案到emulator
adb push
adb pull
ex:
adb push ~/mp3/audio.mp3 /sdcard/audio.mp3

linux底下也可以使用,來管理
mount -o loop sdcard.img android_sdcard

(4)進入emulator的shell
adb shell



(5)使用 am - activity manager 啟動 java 程式
usage: am [start|instrument]
       am start [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
                [-c <CATEGORY> [-c <CATEGORY>] ...]
                [-e <EXTRA_KEY> <EXTRA_VALUE> [-e <EXTRA_KEY> <EXTRA_VALUE> ...]
                [-n <COMPONENT>] [-D] [<URI>]
       am instrument [-e <ARG_NAME> <ARG_VALUE>] [-p <PROF_FILE>]
                [-w] <COMPONENT>

啟動瀏覽器:
# am start -n com.google.android.browser/com.google.android.browser.BrowserActivity

am start -n com.google.android.browser/com.google.android.browser.BrowserActivity
Starting: Intent { comp={com.google.android.browser/com.google.android.browser.BrowserActivity} }

//啟動瀏覽器,打開目標網址
# am start -a android.intent.action.VIEW -d http://www.google.com
am start -a android.intent.action.VIEW -d http://www.google.com
Starting: Intent { action=android.intent.action.VIEW data=http://www.google.com }

//撥打電話,號碼是123456789
# am start -a android.intent.action.CALL -d tel:123456789
am start -a android.intent.action.CALL -d tel:123456789
Starting: Intent { action=android.intent.action.CALL data=tel:123456789}



 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-20 01:14 , Processed in 0.054893 second(s), 22 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表