【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  

 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 下的数据

5、dumpsys命令

主要用来获取一些系统service的信息,也能够对这些服务做一些简单的控制。

在使用此命令之前要先查看一下有哪些组件是能够使用这个命令的

C:\Users\Administrator>adb shell
shell@android:/ $ dumpsys | grep DUMP
dumpsys | grep DUMP
DUMP OF SERVICE Binder.Pged:
DUMP OF SERVICE ProtocolParaManagerService:
DUMP OF SERVICE SurfaceFlinger:
DUMP OF SERVICE accessibility:
DUMP OF SERVICE account:
DUMP OF SERVICE activity:
DUMP OF SERVICE alarm:
DUMP OF SERVICE appwidget:
DUMP OF SERVICE audio:
DUMP OF SERVICE audio.om:
DUMP OF SERVICE backup:
DUMP OF SERVICE battery:
DUMP OF SERVICE batteryinfo:
DUMP OF SERVICE bluetooth_manager:
DUMP OF SERVICE clipboard:

还有非常多,比方activity的信息

C:\Users\Administrator>adb shell dumpsys activity > e:\activity.txt

C:\Users\Administrator>
因为activity的信息太多了,所以将信息输出到文件。


查看程序内存使用情况,dumpsys meminfo YoupackageName

C:\Users\Administrator>adb shell dumpsys meminfo cn.com.zte.uc
Applications Memory Usage (kB):
Uptime: 2341477 Realtime: 2341464

** MEMINFO in pid 5851 [cn.com.zte.uc] **
                         Shared  Private     Heap     Heap     Heap
                   Pss    Dirty    Dirty     Size    Alloc     Free
                ------   ------   ------   ------   ------   ------
       Native       72       12       72     9012     5361     3650
       Dalvik    28915     5784    28768    28624    26812     1812
       Cursor        0        0        0
       Ashmem        0        0        0
    Other dev        4       60        0
     .so mmap     3334      864     1196
    .jar mmap       20        0        0
    .apk mmap      238        0        0
    .ttf mmap      482        0        0
    .dex mmap     4576        0       28
   Other mmap     1151        4       40
      Unknown     5480       36     5480
        TOTAL    44272     6760    35584    37636    32173     5462

 Objects
               Views:      552         ViewRootImpl:        1
         AppContexts:       14           Activities:       11
              Assets:        4        AssetManagers:        4
       Local Binders:       33        Proxy Binders:       29
    Death Recipients:        0
     OpenSSL Sockets:        0

 SQL
         MEMORY_USED:      352
  PAGECACHE_OVERFLOW:      182          MALLOC_SIZE:       62

 DATABASES
      pgsz     dbsz   Lookaside(b)          cache  Dbname
         4      268            477      539/60/25  /data/data/cn.com.zte.uc/databases/E300

 Asset Allocations
    zip:/data/app/cn.com.zte.uc-1.apk:/resources.arsc: 192K

C:\Users\Administrator>

假设dumpsys不能用,检查你的manifest.xml是不是加了这个permission:
android.permission.DUMP


6、pm命令 (2014年9月26日更新)

     这个命令相同很强大。来看下它都有哪些用处。

usage: pm list packages [-f] [-d] [-e] [-s] [-3] [-i] [-u] [--user USER_ID] [FIL
TER]
       pm list permission-groups
       pm list permissions [-g] [-f] [-d] [-u] [GROUP]
       pm list instrumentation [-f] [TARGET-PACKAGE]
       pm list features
       pm list libraries
       pm list users
       pm path PACKAGE
       pm install [-l] [-r] [-t] [-i INSTALLER_PACKAGE_NAME] [-s] [-f]
                  [--algo <algorithm name> --key <key-in-hex> --iv <IV-in-hex>]
                  [--originating-uri <URI>] [--referrer <URI>] PATH
       pm uninstall [-k] PACKAGE
       pm clear [--user USER_ID] PACKAGE
       pm enable [--user USER_ID] PACKAGE_OR_COMPONENT
       pm disable [--user USER_ID] PACKAGE_OR_COMPONENT
       pm disable-user [--user USER_ID] PACKAGE_OR_COMPONENT
       pm grant PACKAGE PERMISSION
       pm revoke PACKAGE PERMISSION
       pm set-install-location [0/auto] [1/internal] [2/external]
       pm get-install-location
       pm set-permission-enforced PERMISSION [true|false]
       pm trim-caches DESIRED_FREE_SPACE
       pm create-user USER_NAME
       pm remove-user USER_ID
       pm get-max-users

pm list packages: prints all packages, optionally only
  those whose package name contains the text in FILTER.  Options:
    -f: see their associated file.
    -d: filter to only show disbled packages.
    -e: filter to only show enabled packages.
    -s: filter to only show system packages.
    -3: filter to only show third party packages.
    -i: see the installer for the packages.
    -u: also include uninstalled packages.

pm list permission-groups: prints all known permission groups.

pm list permissions: prints all known permissions, optionally only
  those in GROUP.  Options:
    -g: organize by group.
    -f: print all information.
    -s: short summary.
    -d: only list dangerous permissions.
    -u: list only the permissions users will see.

pm list instrumentation: use to list all test packages; optionally
  supply <TARGET-PACKAGE> to list the test packages for a particular
  application.  Options:
    -f: list the .apk file for the test package.

pm list features: prints all features of the system.

pm list users: prints all users on the system.

pm path: print the path to the .apk of the given PACKAGE.

pm install: installs a package to the system.  Options:
    -l: install the package with FORWARD_LOCK.
    -r: reinstall an exisiting app, keeping its data.
    -t: allow test .apks to be installed.
    -i: specify the installer package name.
    -s: install package on sdcard.
    -f: install package on internal flash.
    -d: allow version code downgrade.

pm uninstall: removes a package from the system. Options:
    -k: keep the data and cache directories around after package removal.

pm clear: deletes all data associated with a package.

pm enable, disable, disable-user: these commands change the enabled state
  of a given package or component (written as "package/class").

pm grant, revoke: these commands either grant or revoke permissions
  to applications.  Only optional permissions the application has
  declared can be granted or revoked.

pm get-install-location: returns the current install location.
    0 [auto]: Let system decide the best location
    1 [internal]: Install on internal device storage
    2 [external]: Install on external media

pm set-install-location: changes the default install location.
  NOTE: this is only intended for debugging; using this can cause
  applications to break and other undersireable behavior.
    0 [auto]: Let system decide the best location
    1 [internal]: Install on internal device storage
    2 [external]: Install on external media

pm trim-caches: trim cache files to reach the given free space.

pm create-user: create a new user with the given USER_NAME,
  printing the new user identifier of the user.

pm remove-user: remove the user with the given USER_IDENTIFIER,
  deleting all data associated with that user
比如:打印全部已知的权限

shell@android:/ $ pm list permissions
pm list permissions
All Permissions:

permission:com.sina.weibo.permission.PUSH_MSG_BROADCAST_PERMISSION
permission:com.google.android.gms.permission.GAMES_DEBUG_SETTINGS
permission:com.tencent.photos.permission.DATA    ..........
shell@android:/ $


额。有点多就省略了,有兴趣就查下。只是这查起来就非常具体了。。还有安装 卸载,清空包,获取/改动安装路径,删除用户,创建用户,清理缓存,查看安装过的包名 等等。。








posted @ 2015-02-06 09:11  zfyouxi  阅读(1436)  评论(0编辑  收藏  举报