【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/bincd /system/bin# lslsBlobCache_testInputChannel_testInputDispatcher_testInputEvent_testInputPublisherAndConsInputReader_testLooper_testObbFile_testString8_testUnicode_testZipFileRO_testadbamapp_processapplypatchapplypatch_staticashaudioloopbmgrbootanimationbubugreportcatcheck_prereqchmodchowncmpdalvikvmdatedddebuggerddecoderdexoptdfdhcpcddmesgdumpstatedumpsysflash_imagefsck_msdosgdbjithelpergdbservergeteventgetpropgziphdidifconfigiftopimeinputinsmodinstalldioctlioniceip6tablesiptableskeystorekeystore_clikilllinkerlnloglogcatlogwrapperlslsmodlsofmediaservermkdirmonkeymountmtpdmvnandreadndcnetcfgnetdnetstatnewfs_msdosnotifyomx_testspingping6pmpppdprintenvpsqemu-propsqemudrracoonradiooptionsrebootrecordrecordvideorecoveryrenicerildrmrmdirrmmodrouterun-asschedtestschedtopscreencapscreenshotsdcardsendeventsensorserviceserviceservicemanagersetconsolesetpropsf2shshowleasesimg2imgskia_testsleepsmdstagefrightstartstopstreamsurfaceflingersvcsyncsystem_servertestid3toolboxtoptouchumountupdateruptimevdcvmstatvoldwatchpropswipe#
噢 my god 命令还是很多的嘛!
我们一般可以通过 命令 -h 来查看介绍,例如:
# am -ham -husage: am [subcommand] [options]usage: am start [-D] [-W] [-P ] [--start-profiler ] [--R COUNT] [-S] am startservice am force-stop am kill am kill-all am broadcast am instrument [-r] [-e ] [-p ] [-w] [--no-window-animation] am profile [looper] start am profile [looper] stop [] am dumpheap [flags] am set-debug-app [-w] [--persistent] am clear-debug-app am monitor [--gdb ] am screen-compat [on|off] 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 : start profiler and send results to -P : like above, but profiling stops when app goes idle -R: repeat the activity launch times. Prior to each repeat, the top activity will be finished. -S: force stop the target app before starting the activityam startservice: start a Service.am force-stop: force stop everything associated with .am kill: Kill all processes associated with . 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 is the form /. Options are: -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT). Use with [-e perf true] to generate raw output for performance measurements. -e : set argument to . For test runners a common form is [-e [,...]]. -p : write profiling data to -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 heapam set-debug-app: set application to debug. Options are: -w: wait for debugger when application starts --persistent: retain this valueam 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/ANRam screen-compat: control screen compatibility mode of .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. specifications include these flags and arguments: [-a ] [-d ] [-t ] [-c [-c ] ...] [-e|--es ...] [--esn ...] [--ez ...] [--ei ...] [--el ...] [--eu ...] [--eia [, [,] [-f ] [--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] [ | | ]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
wipe表示清除模拟器或者真机上的数据,比如你的模拟器用了很久,装了很多软件就可以用这个来清除
system表示清除 /system下的数据
data表述清除 /data 下的数据
5、dumpsys命令
主要用来获取一些系统service的信息,也可以对这些服务做一些简单的控制。
在使用此命令之前要先查看一下有哪些组件是可以使用这个命令的
C:\Users\Administrator>adb shellshell@android:/ $ dumpsys | grep DUMPdumpsys | grep DUMPDUMP 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.txtC:\Users\Administrator>
由于activity的信息太多了,所以将信息输出到文件。 查看程序内存使用情况,dumpsys meminfo YoupackageName
C:\Users\Administrator>adb shell dumpsys meminfo cn.com.zte.ucApplications 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: 192KC:\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] [FILTER] 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 --key --iv ] [--originating-uri ] [--referrer ] 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-userspm 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 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 mediapm 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 mediapm 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 permissionspm list permissionsAll Permissions:permission:com.sina.weibo.permission.PUSH_MSG_BROADCAST_PERMISSIONpermission:com.google.android.gms.permission.GAMES_DEBUG_SETTINGSpermission:com.tencent.photos.permission.DATA ..........shell@android:/ $
更多相关文章
- Android 传输数据时Base64编码算法详解
- Android SQLite数据库 《第一行代码》
- [置顶] Android中数据存储的5种方法
- Android webview与js交换JSON对象数据示例
- Android周学习Step By Step(6)--Android的数据库SQLite
- [Android]为Spinner填充数据后设置默认值的问题
- Android中使用SQlite进行数据操作
- Android Touch事件原理加实例分析