当前位置: 首页 > 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
 

http://www.xdnf.cn/news/390403.html

相关文章:

  • 大学之大:苏黎世大学2025.5.11
  • 数字化工厂中央控制室驾驶舱系统 API接口文档
  • go 通过汇编学习atomic原子操作原理
  • iVX 图形化编程平台:结合 AI 原生开发的革新与实践
  • 07.three官方示例+编辑器+AI快速学习webgl_buffergeometry_attributes_integer
  • Python-UV多环境管理
  • 5G-A来了!5G信号多个A带来哪些改变?
  • 经典音乐播放器——完美歌词 Poweramp Music Player 3 build
  • MyBatis进阶:掌握动态SQL,实现灵活的数据库查询
  • 实战项目5(08)
  • 【网络安全】——大端序(Big-Endian)​​和​​小端序(Little-Endian)
  • 【Linux系列】bash_profile 与 zshrc 的编辑与加载
  • 大语言模型通过MCP控制STM32-支持Ollama、DeepSeek、openai等
  • 大模型在肾肿瘤诊疗全流程预测及方案制定中的应用研究
  • 【英语笔记(三)】介绍谓语动词的分类,初步讲解四种基本状态:一般、进行、完成、完成进行
  • C#游戏开发中的注意事项
  • 淘宝19块钱激光雷达SDK转ROS2架构
  • 低代码开发:开启软件开发的新篇章
  • RAID磁盘阵列的概念(自用留档)
  • Redis BigKey 问题是什么
  • 卷积神经网络-从零开始构建一个卷积神经网络
  • PDF2zh插件在zotero中安装并使用
  • FramePack AI图片生成视频 v1.1 整合包
  • c++STL-string的使用
  • Java面试常见技术问题解析
  • 软考冲刺——案例分析题Super VLAN
  • BGP基础配置实验
  • OceanBase性能关键参数配置最佳实践
  • 5.1.1 WPF中Command使用介绍
  • 菜鸟之路day31一一MySQL之多表设计