【Android】cmd命令
Android中cmd命令可以用来向binder服务发送命令,来进行相关调试,
其实现原理是调用binder服务的command接口
frameworks/native/cmds/cmd/cmd.cpp
209 Vector<String16> args;
210 for (int i=2; i<argc; i++) {
211 args.add(String16(argv[i]));
212 }
213 String16 cmd = String16(argv[1]);
214 sp<IBinder> service = sm->checkService(cmd);
215 if (service == NULL) {
216 ALOGW("Can't find service %s", argv[1]);
217 aerr << "cmd: Can't find service: " << argv[1] << endl;
218 return 20;
219 }
220
221 sp<MyShellCallback> cb = new MyShellCallback();
222 sp<MyResultReceiver> result = new MyResultReceiver();
223
224#if DEBUG
225 ALOGD("cmd: Invoking %s in=%d, out=%d, err=%d", argv[1], STDIN_FILENO, STDOUT_FILENO,
226 STDERR_FILENO);
227#endif
228
229 // TODO: block until a result is returned to MyResultReceiver.
230 status_t err = IBinder::shellCommand(service, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, args,
231 cb, result);
232 if (err < 0) {
233 const char* errstr;
234 switch (err) {
235 case BAD_TYPE: errstr = "Bad type"; break;
236 case FAILED_TRANSACTION: errstr = "Failed transaction"; break;
237 case FDS_NOT_ALLOWED: errstr = "File descriptors not allowed"; break;
238 case UNEXPECTED_NULL: errstr = "Unexpected null"; break;
239 default: errstr = strerror(-err); break;
240 }
241 ALOGW("Failure calling service %s: %s (%d)", argv[1], errstr, -err);
242 aout << "cmd: Failure calling service " << argv[1] << ": " << errstr << " ("
243 << (-err) << ")" << endl;
244 return err;
245 }
常用的settings命令,就是使用的cmd命令,
/system/bin $ cat settings
#!/system/bin/sh
cmd settings "$@"
$ settings -h
Settings provider (settings) commands:
help
Print this help text.
get [--user <USER_ID> | current] NAMESPACE KEY
Retrieve the current value of KEY.
put [--user <USER_ID> | current] NAMESPACE KEY VALUE [TAG] [default]
Change the contents of KEY to VALUE.
TAG to associate with the setting.
{default} to set as the default, case-insensitive only for global/secure namespace
delete [--user <USER_ID> | current] NAMESPACE KEY
Delete the entry for KEY.
reset [--user <USER_ID> | current] NAMESPACE {PACKAGE_NAME | RESET_MODE}
Reset the global/secure table for a package with mode.
RESET_MODE is one of {untrusted_defaults, untrusted_clear, trusted_defaults}, case-insensitive
list [--user <USER_ID> | current] NAMESPACE
Print all defined keys.
NAMESPACE is one of {system, secure, global}, case-insensitive
使用方法
通常情况下,你将通过 adb shell 来运行 cmd 命令,基本格式如下:
Bash
深色版本
adb shell cmd <service> <command> [options]
<service>:指定要调用的 Android 系统服务。
<command>:对该服务执行的具体命令。
[options]:根据具体命令可能需要提供的参数或选项。
常见服务及命令
以下是一些常用的 Android 服务以及它们可以执行的一些命令示例:
1. 包管理器(Package Manager)
管理应用程序包。
列出所有已安装的应用程序:
Bash
深色版本
adb shell cmd package list packages
查看特定应用的信息:
Bash
深色版本
adb shell cmd package dump com.example.app
安装一个 APK 包(虽然通常直接使用 adb install):
adb shell cmd package install-existing com.example.app
2. 电源管理(Power)
与设备电源状态相关的命令。
获取当前电池状态:
Bash
深色版本
adb shell cmd battery unplug
3. Wi-Fi
管理和配置 Wi-Fi 设置。
打开 Wi-Fi:
adb shell cmd wifi enable
关闭 Wi-Fi:
adb shell cmd wifi disable
4. 输入法(Input Method)
管理输入法相关设置。
列出所有可用的输入法:
adb shell cmd im list
设置默认输入法:
adb shell cmd im set com.example.inputmethod/.ExampleInputMethodService
5. 通知(Notification)
管理通知相关操作。
清除所有通知:
adb shell cmd notification cancel-all