【Android】-- adb shell 命令探索

时间:2022-01-27 18:30:42

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:/ $

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

【Android】-- adb shell 命令探索的更多相关文章

  1. 【安卓手机通用】android adb shell 命令大全

    浏览:3116 | 更新:2013-10-17 17:05 | 标签:安卓 android 一.[什么是shell] Linux系统的shell作为操作系统的外壳,为用户提供使用操作系统的接口.它是命 ...

  2. Android adb shell命令大全

    1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd 3. 创建AVD(模拟器): androi ...

  3. android adb shell 命令大全

    1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd 3. 创建AVD(模拟器): androi ...

  4. 将常用的Android adb shell 命令行封装为C&num;静态函数

    更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 简介:adb命令是常用的Android命令行,自动化.代码调试.手工排查问题都会用的到,这里将常用的一些命令行封装 ...

  5. 自动化测试系列:将常用的Android adb shell 命令行封装为C&num;静态函数

    更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 简介:adb命令是常用的Android命令行,自动化.代码调试.手工排查问题都会用的到,这里将常用的一些命令行封装 ...

  6. ubuntu 使用adb shell命令识别android设备

    ubuntu 使用adb shell命令配置 在ubuntu下使用adb 命令识别Android设备需配置adb_usb.ini 文件 文件路径:  ~/.android/ ,若不存在创建该文件. a ...

  7. 【原创】Android开发之ADB及ADB SHELl命令的应用

    adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是debug工具.adb的工作方式比 ...

  8. adb shell 命令详解&comma;android

    http://www.miui.com/article-275-1.html http://noobjava.iteye.com/blog/1914348 adb shell 命令详解,android ...

  9. Android 执行 adb shell 命令

    Android 执行Adb shell 命令大多需要root权限,Android自带的Runtime. getRuntime().exec()容易出错,在网上找到了一个执行adb shell命令的类 ...

随机推荐

  1. 4&period;1&sol;4&period;2 多线程进阶篇&lt&semi;上&gt&semi;(Pthread &amp&semi; NSThread)

    本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 本文源码 Demo 详见 Githubhttps://github.com/shorfng ...

  2. 搭建Linux&plus;Jexus&plus;MariaDB&plus;ASP&period;NET&lbrack;LJMA&rsqb;环境

    备注:,将我的博客内容整理成册,首先会在博客里优先发布,后续可能的话整理成电子书,主要从linux的最基础内容开始进入Linux的Mono开发方面的话题.本文是我整理博客内容的一篇文章. LJMA 是 ...

  3. python中迭代器和生成器

    l=[1,2,3,4] for n in l: print n 在看上面这段代码的时候,我们没有显式的控制列表的偏移量,就可以自动的遍历了整个列表对象.那么for 语句是怎么来遍历列表l的呢?要回答这 ...

  4. 2016年10月18日 星期二 --出埃及记 Exodus 19&colon;2

    2016年10月18日 星期二 --出埃及记 Exodus 19:2 After they set out from Rephidim, they entered the Desert of Sina ...

  5. 模糊查询&amp&semi;&amp&semi;日期时间操作

    一.模糊查询 1.采用“_”.“%”通配符进行查询 select * from Students where stu_name like '张_';--一个‘_’表示一个字符 select * fro ...

  6. Attach&lpar;&rpar;函数和Detach&lpar;&rpar;函数的作用

    基本就是把一个句柄绑定和解绑定于一个类对象上,是其可以使用MFC的函数而不是API 首先,你要明白Windows对象和MFC对象的区别.MFC对象实际上并没有把整个Windows对象都包装在其中,它只 ...

  7. 选择排序——Python实现

    选择排序: 选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理如下.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小( ...

  8. 避免Linux上错删文件

    前言 在linux上我们常见的问题就是一个操作不小心误删除文件,而且在linux想要恢复文件没这么简单.只有当每次删除之后才后悔莫及,参考windows中最常见的做法就是给系统装一个回收站,让每次删除 ...

  9. linux添加本地yum源

    增加一块硬盘 然后格式化成btrfs格式的文件 mkfs.btrfs /dev/sde 永久挂载 echo "/dev/sde /mnt btrfs defaults 0 0" & ...

  10. 基于vue-cli的vue项目移动端样式适配,lib-flexible和postcss-px2rem

    安装 flexible和 postcss-px2rem(命令行安装) npm install lib-flexible --save npm install postcss-px2rem --save ...