首页 > 代码库 > 【Android】-- adb shell 命令探索

【Android】-- adb shell 命令探索

ADB是什么,做android开发的没有不知道的。

window下运行cmd,输入adb help就会打印adb都能够做的事情,包括 adb push 。。adb pull 。。 adb devices  adb install。。。 等等。。并且一搜一大把。 


其中有一个命令叫做  adb shell,android是基于linux的,shell模式下linux的命令可是非常多的,android去掉了许多linux的命令,那么从这写命令里面又能够发现什么宝藏呢?寻宝之旅开启!!


首先linux的shell模式有许多命令,例如 ls cd mkdir su ps vim等等许多控制命令,这些在android上都是支持的。但是它是android的内核啊,应该有些特殊点的吧。


通过命令cd /system/bin 进入bin目录查看一下。


# cd /system/bin
cd /system/bin
# ls
ls
BlobCache_test
InputChannel_test
InputDispatcher_test
InputEvent_test
InputPublisherAndCons
InputReader_test
Looper_test
ObbFile_test
String8_test
Unicode_test
ZipFileRO_test
adb
am
app_process
applypatch
applypatch_static
ash
audioloop
bmgr
bootanimation
bu
bugreport
cat
check_prereq
chmod
chown
cmp
dalvikvm
date
dd
debuggerd
decoder
dexopt
df
dhcpcd
dmesg
dumpstate
dumpsys
flash_image
fsck_msdos
gdbjithelper
gdbserver
getevent
getprop
gzip
hd
id
ifconfig
iftop
ime
input
insmod
installd
ioctl
ionice
ip6tables
iptables
keystore
keystore_cli
kill
linker
ln
log
logcat
logwrapper
ls
lsmod
lsof
mediaserver
mkdir
monkey
mount
mtpd
mv
nandread
ndc
netcfg
netd
netstat
newfs_msdos
notify
omx_tests
ping
ping6
pm
pppd
printenv
ps
qemu-props
qemud
r
racoon
radiooptions
reboot
record
recordvideo
recovery
renice
rild
rm
rmdir
rmmod
route
run-as
schedtest
schedtop
screencap
screenshot
sdcard
sendevent
sensorservice
service
servicemanager
setconsole
setprop
sf2
sh
showlease
simg2img
skia_test
sleep
smd
stagefright
start
stop
stream
surfaceflinger
svc
sync
system_server
testid3
toolbox
top
touch
umount
updater
uptime
vdc
vmstat
vold
watchprops
wipe
#

噢 my god 命令还是很多的嘛!

我们一般可以通过 命令 -h 来查看介绍,例如:

# am -h
am -h
usage: am [subcommand] [options]
usage: am start [-D] [-W] [-P <FILE>] [--start-profiler <FILE>]
               [--R COUNT] [-S] <INTENT>
       am startservice <INTENT>
       am force-stop <PACKAGE>
       am kill <PACKAGE>
       am kill-all
       am broadcast <INTENT>
       am instrument [-r] [-e <NAME> <VALUE>] [-p <FILE>] [-w]
               [--no-window-animation] <COMPONENT>
       am profile [looper] start <PROCESS> <FILE>
       am profile [looper] stop [<PROCESS>]
       am dumpheap [flags] <PROCESS> <FILE>
       am set-debug-app [-w] [--persistent] <PACKAGE>
       am clear-debug-app
       am monitor [--gdb <port>]
       am screen-compat [on|off] <PACKAGE>
       am display-size [reset|MxN]
       am to-uri [INTENT]
       am to-intent-uri [INTENT]

am start: start an Activity.  Options are:
    -D: enable debugging
    -W: wait for launch to complete
    --start-profiler <FILE>: start profiler and send results to <FILE>
    -P <FILE>: like above, but profiling stops when app goes idle
    -R: repeat the activity launch <COUNT> times.  Prior to each repeat,
        the top activity will be finished.
    -S: force stop the target app before starting the activity

am startservice: start a Service.

am force-stop: force stop everything associated with <PACKAGE>.

am kill: Kill all processes associated with <PACKAGE>.  Only kills.
  processes that are safe to kill -- that is, will not impact the user
  experience.

am kill-all: Kill all background processes.

am broadcast: send a broadcast Intent.

am instrument: start an Instrumentation.  Typically this target <COMPONENT>
  is the form <TEST_PACKAGE>/<RUNNER_CLASS>.  Options are:
    -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT).  Use with
        [-e perf true] to generate raw output for performance measurements.
    -e <NAME> <VALUE>: set argument <NAME> to <VALUE>.  For test runners a
        common form is [-e <testrunner_flag> <value>[,<value>...]].
    -p <FILE>: write profiling data to <FILE>
    -w: wait for instrumentation to finish before returning.  Required for
        test runners.
    --no-window-animation: turn off window animations will running.

am profile: start and stop profiler on a process.

am dumpheap: dump the heap of a process.  Options are:
    -n: dump native heap instead of managed heap

am set-debug-app: set application <PACKAGE> to debug.  Options are:
    -w: wait for debugger when application starts
    --persistent: retain this value

am clear-debug-app: clear the previously set-debug-app.

am monitor: start monitoring for crashes or ANRs.
    --gdb: start gdbserv on the given port at crash/ANR

am screen-compat: control screen compatibility mode of <PACKAGE>.

am display-size: override display size.

am to-uri: print the given Intent specification as a URI.

am to-intent-uri: print the given Intent specification as an intent: URI.

<INTENT> specifications include these flags and arguments:
    [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
    [-c <CATEGORY> [-c <CATEGORY>] ...]
    [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
    [--esn <EXTRA_KEY> ...]
    [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
    [--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
    [--el <EXTRA_KEY> <EXTRA_LONG_VALUE> ...]
    [--eu <EXTRA_KEY> <EXTRA_URI_VALUE> ...]
    [--eia <EXTRA_KEY> <EXTRA_INT_VALUE>[,<EXTRA_INT_VALUE...]]
    [--ela <EXTRA_KEY> <EXTRA_LONG_VALUE>[,<EXTRA_LONG_VALUE...]]
    [-n <COMPONENT>] [-f <FLAGS>]
    [--grant-read-uri-permission] [--grant-write-uri-permission]
    [--debug-log-resolution] [--exclude-stopped-packages]
    [--include-stopped-packages]
    [--activity-brought-to-front] [--activity-clear-top]
    [--activity-clear-when-task-reset] [--activity-exclude-from-recents]
    [--activity-launched-from-history] [--activity-multiple-task]
    [--activity-no-animation] [--activity-no-history]
    [--activity-no-user-action] [--activity-previous-is-top]
    [--activity-reorder-to-front] [--activity-reset-task-if-needed]
    [--activity-single-top] [--activity-clear-task]
    [--activity-task-on-home]
    [--receiver-registered-only] [--receiver-replace-pending]
    [--selector]
    [<URI> | <PACKAGE> | <COMPONENT>]

Error: Unknown command: -h

虽然-h没法识别,可还是打印出了参数的。

1、am命令是非常有用的一个命令,使用此命令可以启动 activity, services;发送 broadcast等等。

使用实例:如启动一个 Activity:

格式:adb shell am start -n 包名/包名+类名(-n 类名,-a action,-d date,-m MIME-TYPE,-c category,-e 扩展数据,等)。 

实例1:C:\Users\Administrator>adb shell am start -n com.android.camera/.CameraStarting: Intent { cmp=com.android.camera/.Camera } 

实例2:(带extra 的 intent)C:\Users\Administrator>adb shell am start -n com.android.camera/.Camera -e abc helloStarting: Intent { cmp=com.android.camera/.Camera (has extras) }其中 extra 的 key 为 abc ,value 为字串 "hello" 

还可以发送命令模拟手机低电环境:

实例:adb shell am broadcast -a android.intent.action.BATTERY_CHANGED --ei "level" 3 --ei "scale" 100

2、getprop/watchprops

getprop列出系统的属性,前面十名字,后面是值
# getprop
getprop
[ro.secure]: [0]
[ro.allow.mock.location]: [1]
[ro.debuggable]: [1]
[persist.service.adb.enable]: [1]
[ro.kernel.qemu]: [1]
[ro.kernel.console]: [ttyS0]
[ro.kernel.android.checkjni]: [1]
[ro.kernel.android.qemud]: [ttyS1]
[ro.kernel.android.ndns]: [1]
[ro.factorytest]: [0]
[ro.serialno]: []

3、watchprosp动态监视这些属性的变化,比如我修改系统的语言为中文,就会打印出:
# watchprops
watchprops
1269420653 persist.sys.language = ‘zh‘
1269420653 persist.sys.language = ‘CN‘

4、 wipe <system|data|all>
wipe表示清除模拟器或者真机上的数据,比如你的模拟器用了很久,装了很多软件就可以用这个来清除
system表示清除 /system下的数据
data表述清除 /data 下的数据