ADB Shell Common Command Instructions with ShellUtils

ADB Shell common command documentation

1. Instructions for use:

The adb Shell command is the statement that the Android shell enters and executes under linux

There is no need to distinguish between running on Android devices, they are all executed in Runtime.getRuntime().exec("command")

For the use on the PC, it is mainly to distinguish whether to enter the Shell to execute when executing on the PC

Common commands to ensure that the PC and the Android device are successfully connected

​ windows端 adb [cmd]

​ Android [cmd]

Shell command windows side adb shell [cmd]

​ Android [cmd]

2. Common commands

1. shutdown

reboot -p

2 reboot

reboot

3. Restart adbd

大部分设备需要正常执行下面的命令
stop adbd
start adbd

4. Install the app

install [options] [包名]
options:
-r:重新安装现有应用,并保留其数据。
-t:允许安装测试 APK。仅当您运行或调试了应用或者使用了 Android Studio 的 Build > Build APK 命令时,Gradle 才会生成测试 APK。如果是使用开发者预览版 SDK(如果 targetSdkVersion 是字母,而非数字)构建的 APK,那么安装测试 APK 时必须在 install 命令中包含 -t 选项。
-i installer_package_name:指定安装程序软件包名称。
--install-location location:使用以下某个值设置安装位置:
0:使用默认安装位置。
1:在内部设备存储上安装。
2:在外部介质上安装。
-f:在内部系统内存上安装软件包。
-d:允许版本代码降级。
-g:授予应用清单中列出的所有权限。
--fastdeploy:通过仅更新已更改的 APK 部分来快速更新安装的软件包。
--incremental:仅安装 APK 中启动应用所需的部分,同时在后台流式传输剩余数据。如要使用此功能,您必须为 APK 签名,创建一个 APK 签名方案 v4 文件,并将此文件放在 APK 所在的目录中。只有部分设备支持此功能。此选项会强制 adb 使用该功能,如果该功能不受支持,则会失败(并提供有关失败原因的详细信息)。附加 --wait 选项,可等到 APK 完全安装完毕后再授予对 APK 的访问权限。
--no-incremental 可阻止 adb 使用此功能。

5. Normal uninstall application

install [包名]

6. Simulate keys

input keyevent [键值]

keycode meaning

3    HOME 键
4    返回键
5    打开拨号应用
6    挂断电话
24    增加音量
25    降低音量
26    电源键
27    拍照(需要在相机应用里)
64    打开浏览器
82    菜单键
85    播放/暂停
86    停止播放
87    播放下一首
88    播放上一首
122    移动光标到行首或列表顶部
123    移动光标到行末或列表底部	
126    恢复播放
127    暂停播放
164    静音
176    打开系统设置
187    切换应用
207    打开联系人
208    打开日历
209    打开音乐
210    打开计算器
220    降低屏幕亮度
221    提高屏幕亮度
223    系统休眠
224    点亮屏幕
231    打开语音助手
276    如果没有 wakelock 则让系统休眠

3. Shell command

(1) getprop and setprop view and modify system properties

1. Query configuration

全部 getprop
指定 getprop [名称]

2. Modify the system configuration

setprop [key] [value]

3. Modify the screen orientation

setprop persist.screen.rotation [角度]

4. Default USB debugging (only available for Lango devices)

setprop persist.allow.usb.debugging 1

5. Open the port

setprop persist.adb.tcp.port 5555
setprop service.adb.tcp.port 5656[Lonbon]

(2) pm [package manager]

1. Uninstall the application and enter the information under pm (package manager)

pm uninstall [options] [package]
参数说明:
-k:移除软件包后保留数据和缓存目录。
--user [userName]
  remove the user with the given USER_IDENTIFIER,删除用户标识
举例:
删除系统应用:pm uninstall --user 0 [包名]

2. List all packages

参数说明:
-k:移除软件包后保留数据和缓存目录。
--user [userName]
  remove the user with the given USER_IDENTIFIER,删除用户标识
举例:
删除系统应用:pm uninstall --user 0 [包名]

3.debug debugging application

set-debug-app [包名]

For more pm usage, see

https://developer.android.google.cn/studio/command-line/adb?hl=zh_cn#pm

(3) pm [activity manger]

1. Broadcast (for brightness, sound) under am (Activity Manager)

am broadcast [options] [intent]
options参数说明
[--user user_id | all | current]:指定要发送给哪个用户;如果未指定,则发送给所有用户。

intent参数说明:
-a action
指定 intent 操作,如 android.intent.action.VIEW。只能声明一次。
-d data_uri
指定 intent 数据 URI,如 content://contacts/people/1。只能声明一次。
-t mime_type
指定 intent MIME 类型,如 image/png。只能声明一次。
-c category
指定 intent 类别,如 android.intent.category.APP_CONTACTS。
-n component
指定带有软件包名称前缀的组件名称以创建显式 intent,如 com.example.app/.ExampleActivity。
-f flags
向 setFlags() 支持的 intent 添加标记。
--esn extra_key
添加一个空 extra。URI intent 不支持此选项。
-e | --es extra_key extra_string_value
以键值对的形式添加字符串数据。
--ez extra_key extra_boolean_value
以键值对的形式添加布尔值数据。
--ei extra_key extra_int_value
以键值对的形式添加整数型数据。
--el extra_key extra_long_value
以键值对的形式添加长整型数据。
--ef extra_key extra_float_value
以键值对的形式添加浮点型数据。
--eu extra_key extra_uri_value
以键值对的形式添加 URI 数据。
--ecn extra_key extra_component_name_value
添加组件名称,该名称作为 ComponentName 对象进行转换和传递。
--eia extra_key extra_int_value[,extra_int_value...]
添加整数数组。
--ela extra_key extra_long_value[,extra_long_value...]
添加长整数数组。
--efa extra_key extra_float_value[,extra_float_value...]
添加浮点数数组。
--grant-read-uri-permission
添加 FLAG_GRANT_READ_URI_PERMISSION 标记。
--grant-write-uri-permission
添加 FLAG_GRANT_WRITE_URI_PERMISSION 标记。
--debug-log-resolution
添加 FLAG_DEBUG_LOG_RESOLUTION 标记。
--exclude-stopped-packages
添加 FLAG_EXCLUDE_STOPPED_PACKAGES 标记。
--include-stopped-packages
添加 FLAG_INCLUDE_STOPPED_PACKAGES 标记。
--activity-brought-to-front
添加 FLAG_ACTIVITY_BROUGHT_TO_FRONT 标记。
--activity-clear-top
添加 FLAG_ACTIVITY_CLEAR_TOP 标记。
--activity-clear-when-task-reset
添加 FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET 标记。
--activity-exclude-from-recents
添加 FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS 标记。
--activity-launched-from-history
添加 FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY 标记。
--activity-multiple-task
添加 FLAG_ACTIVITY_MULTIPLE_TASK 标记。
--activity-no-animation
添加 FLAG_ACTIVITY_NO_ANIMATION 标记。
--activity-no-history
添加 FLAG_ACTIVITY_NO_HISTORY 标记。
--activity-no-user-action
添加 FLAG_ACTIVITY_NO_USER_ACTION 标记。
--activity-previous-is-top
添加 FLAG_ACTIVITY_PREVIOUS_IS_TOP 标记。
--activity-reorder-to-front
添加 FLAG_ACTIVITY_REORDER_TO_FRONT 标记。
--activity-reset-task-if-needed
添加 FLAG_ACTIVITY_RESET_TASK_IF_NEEDED 标记。
--activity-single-top
添加 FLAG_ACTIVITY_SINGLE_TOP 标记。
--activity-clear-task
添加 FLAG_ACTIVITY_CLEAR_TASK 标记。
--activity-task-on-home
添加 FLAG_ACTIVITY_TASK_ON_HOME 标记。
--receiver-registered-only
添加 FLAG_RECEIVER_REGISTERED_ONLY 标记。
--receiver-replace-pending
添加 FLAG_RECEIVER_REPLACE_PENDING 标记。
--selector
需要使用 -d 和 -t 选项设置 intent 数据和类型。
URI component package
如果不受上述任一选项的限制,您可以直接指定 URI、软件包名称和组件名称。当某个参数不受限制时,如果该参数包含“:”(冒号),那么该工具会假定参数是 URI;如果该参数包含“/”(正斜线),那么该工具会假定参数是组件名称;如果并非上述两种情况,那么该工具会假定参数是软件包名称。

For more information, please refer to "Intent Parameter Usage Specification":
https://developer.android.google.cn/studio/command-line/adb?hl=zh_cn#IntentSpec

For more am usage see

https://developer.android.google.cn/studio/command-line/adb?hl=zh_cn#am

(4) Others

1. Change to another user's identity (some commands require user permissions, such as modifying system/app

su [-fmp] [-c command] [-s shell] [--help] [--version] [-] [USER [ARG]]
参数说明:

-f 或 --fast 不必读启动档(如 csh.cshrc 等),仅用于 csh 或 tcsh
-m -p 或 --preserve-environment 执行 su 时不改变环境变数
-c command 或 --command=command 变更为帐号为 USER 的使用者并执行指令(command)后再变回原来使用者
-s shell 或 --shell=shell 指定要执行的 shell (bash csh tcsh 等),预设值为 /etc/passwd 内的该使用者(USER) shell
--help 显示说明文件
--version 显示版本资讯
- -l 或 --login 这个参数加了之后,就好像是重新 login 为该使用者一样,大部份环境变数(HOME SHELL USER等等)都是以该使用者(USER)为主,并且工作目录  也会改变,如果没有指定 USER ,内定是 root
USER 欲变更的使用者帐号
ARG 传入新的 shell 参数

example

su -c start adbd

4. Util code

object ShellUtils {
    
    
    fun execute(vararg command: String): ShellResult {
    
    
        val process = Runtime.getRuntime().exec(command)
        val success = process.waitFor() == 0
        val iStream = if (success) process.inputStream else process.errorStream
        val output = CharStreams.toString(InputStreamReader(iStream))
        return ShellResult(success, output)
    }
}

data class ShellResult(
    val success: Boolean,
    val output: String
)

Guess you like

Origin blog.csdn.net/qq_39178733/article/details/125563299