当前位置: 首页 > news >正文

【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
 

相关文章:

  • 使用 FastAPI 和 MongoDB 实现分页查询功能,并在 React 中进行分页展示
  • 《Hadoop 权威指南》笔记
  • LabVIEW车牌自动识别系统
  • 比亚迪全栈自研生态的底层逻辑
  • C语言_函数调用栈的汇编分析
  • 每日c/c++题 备战蓝桥杯(P1002 [NOIP 2002 普及组] 过河卒)
  • 【Mac 从 0 到 1 保姆级配置教程 12】- 安装配置万能的编辑器 VSCode 以及常用插件
  • 外网访问内网海康威视监控视频的方案:WebRTC + Coturn 搭建
  • 微服务架构中如何保证服务间通讯的安全
  • postgres--MVCC
  • MySQL基础面试题集锦
  • ssh -T git@github.com 测试失败解决方案:修改hosts文件
  • 六大设计模式--OCP(开闭原则):构建可扩展软件的基石
  • AUTOSAR图解==>AUTOSAR_TR_AIDesignPatternsCatalogue
  • 从0开始学linux韦东山教程第三章问题小结(3)
  • 微服务架构实战:从服务拆分到RestTemplate远程调用
  • 模型过拟合是什么?
  • springboot3整合SpringSecurity实现登录校验与权限认证
  • 理解反向Shell:隐藏在合法流量中的威胁
  • React 从零到一执行原理 (2025 最新版)
  • 中国巴西民间推动建立经第三方验证的“森林友好型”牛肉供应链
  • 中美日内瓦经贸会谈联合声明
  • 中方发布会:中美经贸高层会谈取得了实质性进展,达成了重要共识
  • ​中国超大规模市场是信心所在——海南自贸港建设一线观察
  • 广西百色通报:极端强对流天气致墙体倒塌,3人遇难7人受伤
  • 面对非专业人士,科学家该如何提供建议