Injecting Input And State 标志

Injecting Input And State

社区
skydoves
injecting-input-and-state

使用此技能,通过主机 Shell 操控 Android 设备——使用 `adb shell input` 注入点击、滑动、文本、按键事件和拖放操作;使用 `wm size` / `wm density` 调整显示尺寸;使用 `settings put global window_animation_scale 0` 切换彻底测试所需的设置;使用 `pm clear` 与 `am force-stop` 重置应用状态;使用 `am start -n pkg/.Activity` 启动 Activity;在 API 30 及以上版本使用现代的 `cmd <service>` 封装命令(`cmd wifi`、`cmd connectivity`、`cmd uimode night`、`cmd locale set-app-locales`),替代已弃用的 `svc` 别名。如果用户提到“input tap”、“input swipe”、“input text 中的空格”、“KEYCODE_BACK”、“用于屏幕尺寸矩阵的 wm size”、“settings put animation_scale”、“force-stop 与 pm clear 的区别”、“svc wifi 无操作”、“cmd wifi set-wifi-enabled”或“am start intent extras”,请使用此技能。

Add to TypingMind

概览

发布者skydoves
代码仓库android-testing-skills
技能名称injecting-input-and-state
星标数
338
复刻数
14
附带文件
仅含指令
许可协议Apache-2.0
链接
  • Markdown 格式的指令

    模型按需加载 SKILL.md 文件,因此只有在请求确实与该技能匹配时才会消耗 Token。

  • 兼容任何大语言模型

    AI 技能采用纯 Markdown,而非特定提供商的代码,因此可搭配 GPT、Claude、Gemini、Grok 或本地模型使用。

  • 指令自成一体

    模型所需的一切都包含在指令中,无需同步额外文件。

  • 开源

    由 skydoves 发布于 GitHub。请在安装前查看技能的源文件。

安装

在 TypingMind 中安装 Injecting Input And State AI 技能,即可搭配任意大语言模型使用;也可以将其放入其他支持读取 SKILL.md 的智能体中。

1

安装到 TypingMind

TypingMind 可直接从 GitHub 文件夹安装技能:读取 SKILL.md,打包资源文件,并将结果保存在本地。

  1. 打开应用,前往“插件 → 技能”。
  2. 选择“从 GitHub 安装”。
  3. 粘贴下方的技能文件夹 URL 并确认。
    https://github.com/skydoves/android-testing-skills/tree/main/adb/control/injecting-input-and-state
  4. 请在每个需要使用该技能的聊天中启用它。
依次选择“插件 → 技能 → 添加技能 → 从 GitHub URL 导入”,然后粘贴文件夹 URL 并点击“继续”。
2

安装到其他智能体

任何支持 Agent Skills 格式的智能体都可以使用此技能,只需将文件夹复制到该智能体的技能目录中。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/skydoves/android-testing-skills.git /tmp/android-testing-skills
mkdir -p .claude/skills
cp -r /tmp/android-testing-skills/adb/control/injecting-input-and-state .claude/skills/injecting-input-and-state
复制后,请重启 Claude Code,以加载新技能。

在 TypingMind 中使用此技能

在任意 TypingMind 聊天中启用 Injecting Input And State,后续即可交由模型处理。技能的名称和描述会放在系统提示词中,一旦请求匹配,模型就会自动加载完整指令,无需您手动调用,且在实际使用前不会消耗 Token。

一旦请求与技能匹配,模型就会自动加载 Injecting Input And State。

兼容任何 AI 模型

AI 技能是纯 Markdown 指令,而非特定提供商的代码,因此 Injecting Input And State 不局限于最初适配的模型。只需在 TypingMind 中安装一次,即可搭配 GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 或您自行运行的本地模型使用,全程使用您自己的 API 密钥。

  • 仅在需要时加载

    系统提示词中仅包含技能名称和描述。首次遇到匹配的请求时才会加载完整指令,因此技能未使用时,这些指令不会消耗 Token。

  • 在聊天中切换模型

    技能是指令而非代码,因此切换模型不会导致技能失效,新模型会读取同一个 SKILL.md。

技能指令

这是模型会加载的 SKILL.md 内容。请在安装前阅读,因为技能就是模型将遵循的指令。

Injecting Input and State — Drive the Device From the Shell

This skill covers the on-device shell surface a test/QA engineer drives over adb to simulate user gestures, override the screen configuration, flip system settings, reset app state, and launch Activities. Transport (adb devices, USB/TCP) lives in ../../architecture/understanding-adb-architecture/SKILL.md; capturing artefacts after driving the device lives in ../../capture/capturing-screenshots-and-screenrecord/SKILL.md.

When to use this skill

  • The developer needs to script a user journey from CI without writing instrumentation: tap, swipe, type text, press hardware keys.
  • A bug repro requires a specific screen size or density (wm size 411x731, wm density 320) without recreating an emulator AVD.
  • The test suite is flaky because animations are on; the developer wants the canonical hermetic preamble (window_animation_scale = 0 etc.).
  • The developer needs to clear app data between runs and is unsure whether am force-stop is enough (it is not).
  • A script uses adb shell svc wifi disable and silently does nothing on API 30+ — the modern cmd wifi set-wifi-enabled disabled is the fix.
  • The developer needs to launch a deep-link Activity with typed extras (--ez, --ei, --es).

When NOT to use this skill

  • The bytes need to leave the device (screenshots, logs, DB). Use ../../capture/capturing-screenshots-and-screenrecord/SKILL.md, ../../observability/extracting-logs-with-logcat/SKILL.md, ../../transfer/extracting-test-artifacts/SKILL.md.
  • Driving the device from inside an instrumentation test, not from the host. Use ../../../instrumentation/scenarios/launching-activities-with-activityscenario/SKILL.md or UiAutomator.
  • Installing or uninstalling APKs. Use ../../apps/installing-and-managing-apps/SKILL.md.
  • Connecting to the device in the first place. Use ../../devices/connecting-to-devices/SKILL.md or ../../devices/connecting-over-wifi/SKILL.md.

Prerequisites

  • A reachable device or emulator (adb get-state returns device).
  • Platform Tools 23+ on the host. Quoting through adb shell is double-shell (ssh-style) since Platform Tools 23 — load-bearing for any string with spaces (developer.android.com/tools/adb#shellcommands).
  • For cmd wrapper APIs and reliable shell exit codes: device API 24+ (cmd introduced API 24).

input — synthesised user events

input is the on-device wrapper around InputManager.injectInputEvent(). The full grammar:

input [<source>] <command> [<arg>...]

<source> defaults per command (touchscreen for tap/swipe, keyboard for text/keyevent).

Tap, swipe, draganddrop

bash
adb shell input tap <x> <y>
adb shell input swipe <x1> <y1> <x2> <y2> [<duration_ms>]
adb shell input draganddrop <x1> <y1> <x2> <y2> [<duration_ms>]
adb shell input roll <dx> <dy>                        # rotary/trackball axes
adb shell input motionevent <DOWN|UP|MOVE|CANCEL> <x> <y>

swipe defaults to ~300 ms when <duration_ms> is omitted. To synthesise a long-press, swipe in place for the long-press timeout: input swipe <x> <y> <x> <y> 1000. draganddrop differs from swipe in that it emits the drag-and-drop gesture sequence (DOWN → MOVE → UP) consumable by View.OnDragListener, not the same event stream as swipe.

Text input — quoting trap

bash
adb shell input text "<string>"

Caveats (from developer.android.com/tools/adb):

  • Spaces MUST be encoded as %s. input text "hello world" types hello, then nothing.
  • Shell metacharacters (', ", &, (, ), <, >, |, ;, \, backtick) MUST be escaped twice — once for the local shell, once for the device-side shell.
  • Non-ASCII / Unicode is unreliable through input text. Use UiAutomator UiObject2.setText(...) for those.

Key events

bash
adb shell input keyevent [--longpress|--doubletap] <KEYCODE> [<KEYCODE>...]
adb shell input keyevent <NUMERIC_CODE>

Test-relevant KEYCODE_* (KeyEvent constants from frameworks/base; full table in research note A2):

MnemonicNumericNotes
KEYCODE_HOME3Press Home (does not always wake screen).
KEYCODE_BACK4Back button.
KEYCODE_DPAD_UP/DOWN/LEFT/RIGHT/CENTER19-23TV / focus traversal.
KEYCODE_VOLUME_UP/DOWN/MUTE24/25/164
KEYCODE_POWER26Toggles screen off/on.
KEYCODE_TAB61
KEYCODE_ENTER66
KEYCODE_DEL67Backspace.
KEYCODE_MENU82
KEYCODE_APP_SWITCH187Recents button.
KEYCODE_WAKEUP224Wake without unlocking.
KEYCODE_SLEEP223Force sleep.

Wake + dismiss-keyguard for emulator with no lock:

bash
adb shell input keyevent KEYCODE_WAKEUP
adb shell input keyevent 82                     # menu, dismiss the AOSP swipe-up

Source prefixes

input <source> <command> maps to InputDevice.SOURCE_*. Use a source prefix when a screen has multiple input listeners or when targeting non-touch hardware:

SourceTypical use
touchscreenDefault for tap, swipe, draganddrop.
touchpadExternal touchpads (Chromebook / Android tablet keyboard).
dpadTV/focus traversal: input dpad keyevent KEYCODE_DPAD_DOWN.
keyboardDefault for text, keyevent.
mouseMouse buttons + scroll.
trackballTrackball roll.
gamepad / joystickGamepad buttons / joystick axes.
stylusPressure-aware touch (S-Pen).

wm — display size and density overrides

wm is the shell wrapper for IWindowManager. The test-relevant subcommands:

bash
adb shell wm size                               # read current logical size
adb shell wm size 1080x1920                     # override
adb shell wm size reset                         # restore device default

adb shell wm density                            # read current dpi
adb shell wm density 320                        # override
adb shell wm density reset

adb shell wm dismiss-keyguard                   # insecure keyguard only
adb shell wm user-rotation [free|lock [<rotation>]]   # 0,1,2,3 = 0/90/180/270

Use case — screen-size / density matrix without recreating the emulator:

bash
# Per configuration in the matrix
adb shell wm size 411x731
adb shell wm density 240
# … run tests …
adb shell wm size reset
adb shell wm density reset

Caveats:

  • MUST call reset in @AfterClass / suite teardown — otherwise the device stays in the modified configuration after the run, breaking later runs and any human user.
  • wm size / wm density trigger a configuration change that recreates the foreground Activity. Treat them as between-test knobs, not mid-test.
  • am display-size / am display-density are equivalent legacy spellings; wm is preferred.

settings — hermetic test state

bash
adb shell settings [--user <id>] get   <namespace> <key>
adb shell settings [--user <id>] put   <namespace> <key> <value> [<tag>] [default]
adb shell settings [--user <id>] delete <namespace> <key>
adb shell settings [--user <id>] reset <namespace> [<package>] [untrusted_defaults|untrusted_clear|trusted_defaults]
adb shell settings [--user <id>] list  <namespace>

<namespace> is one of global, system, secure.

The hermetic preamble — animation knobs

Set all three to 0 for stable instrumented and screenshot tests:

bash
adb shell settings put global window_animation_scale 0
adb shell settings put global transition_animation_scale 0
adb shell settings put global animator_duration_scale 0

Or via Gradle: testOptions.animationsDisabled = true.

Other test-relevant keys

NamespaceKeyPurpose
systemfont_scaleFloat (e.g. 1.0, 1.3); type-scale tests.
secureaccessibility_enabled1 to enable a11y; pair with enabled_accessibility_services.
secureenabled_accessibility_servicesCSV pkg/.Service for a11y tests.
securedefault_input_methodForce a known IME.
secureshow_ime_with_hard_keyboard1 to keep IME visible on emulators.
securelong_press_timeoutStabilise long-press tests.
globalairplane_mode_onPair with am broadcast android.intent.action.AIRPLANE_MODE.
globalpolicy_controlE.g. immersive.full=* to hide system bars.

App state — am force-stop vs pm clear

CommandKills processes?Clears data/cache/prefs?Resets runtime permissions?
am force-stop <pkg>YesNoNo
pm clear <pkg>Yes (implicitly)YesYes
cmd activity stop-app <pkg>Yes (modern, API 28+)NoNo

For a hermetic between-test reset, use pm clear. For "kill the process so the next launch is cold", am force-stop is enough. See ../../apps/installing-and-managing-apps/SKILL.md for the full pm surface.

Launching Activities — am start

bash
# Most reliable for tests: explicit component.
adb shell am start -n com.example.app/.MainActivity

# Force-stop first, then launch (clean cold start).
adb shell am start -S -W -n com.example.app/.MainActivity

# Launch by action + URI (default handler for https://).
adb shell am start -a android.intent.action.VIEW -d 'https://example.com'

# Pass typed extras.
adb shell am start -n com.example.app/.MainActivity \
  --es screen "settings" --ez debug true --ei count 42

# Return to launcher between tests.
adb shell am start -a android.intent.action.MAIN -c android.intent.category.HOME

Intent specifier flags (abridged from developer.android.com/tools/adb#IntentSpec):

TokenEffect
-a actionIntent action (e.g. android.intent.action.VIEW).
-d data_uriData URI.
-t mime_typeMIME type.
-c categoryCategory.
-n componentExplicit component (pkg/.Activity).
-f flagsHex setFlags() value.
`--es-e key value`
--ez key boolBoolean extra.
--ei key intInt extra.
--el key longLong extra.
--ef key floatFloat extra.
--eu key uriURI extra.
--ecn key componentComponentName extra.
--eia/--ela/--efa key v[,v...]Array extras.
--activity-clear-top etc.Map to FLAG_ACTIVITY_*.
-SForce-stop the target before starting.
-WWait for launch to complete.

cmd <service> — the modern wrapper

cmd is a generic wrapper that calls into a binder service's onShellCommand. Available since API 24. To list services: adb shell cmd -l (or dumpsys -l). Test-relevant entry points:

InvocationEffect
cmd activity stop-app <pkg>Modern equivalent of am force-stop.
cmd activity broadcast …Same as am broadcast.
cmd package install -r -t -g <path>pm install equivalent.
cmd package compile -m speed -f <pkg>Force AOT compilation.
`cmd appops set allowdeny
cmd jobscheduler run -f <pkg> <jobId>Force a JobService to run now.
`cmd uimode night yesno
cmd locale set-app-locales <pkg> --locales en-USPer-app locales (Android 13+).
cmd statusbar expand-notifications / collapseExpand/collapse the shade.
`cmd wifi set-wifi-enabled enableddisabled`
`cmd connectivity airplane-mode enabledisable`
cmd testharness enableReset device to a hermetic test-harness state (Android 10+).

svc deprecation on API 30+

svc data, svc wifi, svc bluetooth are no-ops on API 30+ unless invoked from a privileged shell (corpus §I.10). The preferred replacements are the cmd wrappers above.

Patterns

Pattern: WRONG — svc wifi disable on API 30+

bash
# WRONG
adb shell svc wifi disable
# WRONG because: silently no-ops on API 30+ since the svc CLI was demoted from a privileged
# helper to an ordinary shell command. Exit code 0, Wi-Fi still on. The script proceeds
# under a wrong assumption — exactly the worst kind of failure mode.
bash
# RIGHT
adb shell cmd wifi set-wifi-enabled disabled
adb shell cmd wifi status                       # verify

Pattern: WRONG — am force-stop for a between-test reset

bash
# WRONG
adb shell am force-stop com.example.app
./run-next-test.sh
# WRONG because: am force-stop kills the process but leaves shared prefs, databases, the
# image cache, and granted runtime permissions intact. The "next test" inherits state
# from the previous run.
bash
# RIGHT
adb shell pm clear com.example.app
adb shell pm clear com.example.app.test         # also reset test process state
./run-next-test.sh

(Test Orchestrator with clearPackageData: 'true' is the equivalent inside Gradle — see ../../tests/running-instrumented-tests-via-adb/SKILL.md.)

Pattern: WRONG — input text "hello world"

bash
# WRONG
adb shell input text "hello world"
# WRONG because: spaces in `input text` MUST be encoded as %s. The above types only "hello".
bash
# RIGHT
adb shell input text 'hello%sworld'

Pattern: hermetic preamble + Activity launch

bash
adb shell settings put global window_animation_scale 0
adb shell settings put global transition_animation_scale 0
adb shell settings put global animator_duration_scale 0
adb shell pm clear com.example.app
adb shell am start -S -W -n com.example.app/.MainActivity

-S force-stops first; -W blocks until the launch completes.

Pattern: TV / focus navigation via dpad

bash
adb shell input dpad keyevent KEYCODE_DPAD_DOWN
adb shell input dpad keyevent KEYCODE_DPAD_DOWN
adb shell input dpad keyevent KEYCODE_DPAD_CENTER

Source-prefixing dpad ensures the events route to TV-style focus handlers, not the touchscreen pipeline.

Mandatory rules

  • MUST use cmd wifi set-wifi-enabled / cmd connectivity airplane-mode / cmd bluetooth_manager enable on API 30+. MUST NOT rely on svc wifi / svc bluetooth / svc data — they no-op silently on modern releases.
  • MUST use pm clear <pkg> (not am force-stop) when the goal is a hermetic between-test reset. force-stop only kills processes; data is untouched.
  • MUST encode spaces in input text as %s; MUST double-shell quote (ssh-style) any string with shell metacharacters. Platform Tools 23+ (developer.android.com/tools/adb#shellcommands).
  • MUST call wm size reset and wm density reset in suite teardown when the suite overrode them. A leaked override breaks every subsequent run on that device.
  • MUST set the three animation scales to 0 (or use testOptions.animationsDisabled = true in Gradle) before any UI-asserting test run.
  • MUST NOT use Thread.sleep between adb shell input calls to "let the UI catch up". Inside an instrumentation test, use Compose's idle synchronisation (../../../compose/synchronization/synchronizing-with-idle/SKILL.md); from a host script, use adb shell am start -W to block on Activity launch and adb shell wait-for-device between reboots.
  • MUST NOT assume adb shell am instrument returns a meaningful exit code without -w. See ../../tests/running-instrumented-tests-via-adb/SKILL.md and ../../automation/scripting-adb-for-ci/SKILL.md.
  • PREFERRED: am start -S -W -n pkg/.Activity for cold-start launches in tests — -S resets, -W blocks until ready.

Verification

  • adb shell settings get global window_animation_scale returns 0 (or 0.0) before tests run; same for transition_animation_scale and animator_duration_scale.
  • No script in scripts/ or .github/workflows/ uses svc wifi / svc bluetooth / svc data.
  • Every am force-stop between tests is paired with (or replaced by) pm clear.
  • No input text "<string with spaces>" exists; all spaces are %s.
  • Every wm size <override> in setup has a matching wm size reset in teardown.
  • CI passes 50 consecutive runs without a "device entered prior config" flake.

References

  • developer.android.com/tools/adb — am, pm, input, wm, settings, intent specs (https://developer.android.com/tools/adb).
  • developer.android.com/tools/adb#shellcommands — quoting through adb shell since Platform Tools 23.
  • developer.android.com/reference/android/view/KeyEvent — KEYCODE_* constants table.
  • developer.android.com/training/testing/instrumented-tests/stability — the canonical animation-scale-zero recipe.
  • developer.android.com/studio/test/advanced-test-setup#use-gradle-managed-devices — testOptions.animationsDisabled.
  • Research note tasks/research/A2-adb-shell-commands.md — full input / wm / settings / am / pm / cmd / svc tables and verbatim quotes.
  • Sibling skill: ../../architecture/understanding-adb-architecture/SKILL.md — three-piece architecture, server, daemon.
  • Sibling skill: ../../devices/connecting-to-devices/SKILL.md — adb devices states, wait-for-device.
  • Sibling skill: ../../devices/connecting-over-wifi/SKILL.md — adb pair / adb connect.
  • Sibling skill: ../../apps/installing-and-managing-apps/SKILL.md — full pm install / pm uninstall / pm grant surface.
  • Sibling skill: ../../tests/running-instrumented-tests-via-adb/SKILL.md — am instrument -w -r.
  • Sibling skill: ../../capture/capturing-screenshots-and-screenrecord/SKILL.md — capturing artefacts after driving the device.
  • Sibling skill: ../../observability/extracting-logs-with-logcat/SKILL.md — reading device logs.
  • Sibling skill: ../../transfer/extracting-test-artifacts/SKILL.md — adb pull / adb push / run-as.
  • Sibling skill: ../../automation/scripting-adb-for-ci/SKILL.md — bash idioms, retries, port forwarding.
  • Cross-set: ../../../instrumentation/scenarios/launching-activities-with-activityscenario/SKILL.md — launching from inside an instrumentation test.
  • Cross-set: ../../../fundamentals/strategies/applying-testing-strategies/SKILL.md — when to drive via adb vs instrumentation.

常见问题

Injecting Input And State AI 技能有什么作用?

使用此技能,通过主机 Shell 操控 Android 设备——使用 `adb shell input` 注入点击、滑动、文本、按键事件和拖放操作;使用 `wm size` / `wm density` 调整显示尺寸;使用 `settings put global window_animation_scale 0` 切换彻底测试所需的设置;使用 `pm clear` 与 `am force-stop` 重置应用状态;使用 `am start -n pkg/.Activity` 启动 Activity;在 API 30 及以上版本使用现代的 `cmd <service>` 封装命令(`cmd wifi`、`cmd connectivity`、`cmd uimode night`、`cmd locale set-app-locales`),替代已弃用的 `svc` 别名。如果用户提到“input tap”、“input swipe”、“input text 中的空格”、“KEYCODE_BACK”、“用于屏幕尺寸矩阵的 wm size”、“settings put animation_scale”、“force-stop 与 pm clear 的区别”、“svc wifi 无操作”、“cmd wifi set-wifi-enabled”或“am start intent extr...

为什么在 TypingMind 中使用 Injecting Input And State?

因为您只需安装一次,就能搭配任何模型使用。Injecting Input And State 采用纯 Markdown 格式,而非特定提供商专用的代码,因此同一技能可用于 GPT-5、Claude、Gemini、Grok 或本地模型。您甚至可以在聊天中途切换模型,而不会影响技能的正常使用。TypingMind 使用您自己的 API 密钥,因此您直接向提供商付费,而不是按席位订阅;您的技能和聊天记录也都保留在您自己的存储空间中。

如何在 TypingMind 中安装 Injecting Input And State?

在 TypingMind 中打开“插件 → 技能 → 从 GitHub 安装”,然后粘贴 https://github.com/skydoves/android-testing-skills/tree/main/adb/control/injecting-input-and-state。TypingMind 会读取其中的 SKILL.md,并将其安装为技能,供您在各个聊天中按需启用。

哪些 AI 模型可以使用 Injecting Input And State?

您在 TypingMind 中连接的任何模型都可以。AI 技能采用纯 Markdown 指令,而非特定提供商专用的代码,因此 GPT、Claude、Gemini、Grok 和本地模型都能在请求匹配时加载此技能。

Injecting Input And State 可以搭配多少个 AI 模型使用?

数量不限。只要模型支持技能,您就可以将它与 Injecting Input And State 搭配使用。GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 等模型都可以通过您自己的 API 密钥接入 TypingMind。

Injecting Input And State AI 技能免费吗?

是的。skydoves 已在 GitHub 上根据 Apache-2.0 许可证发布了该技能。您只需向自己的 AI 提供商支付所用 Token 的费用。

什么是 AI 技能?

AI 技能是一组可复用的指令,用于教会 AI 模型完成某项特定任务。它遵循开放的 Agent Skills 格式:包含名称和描述的 SKILL.md 文件,以及模型可能需要的脚本、模板或参考文件。模型仅在您的请求与技能匹配时才会读取这些指令,因此已安装的技能在使用前不会产生任何费用。

AI 技能与插件或 MCP 服务器有何不同?

插件或 MCP 服务器为模型提供可调用的新工具,也就是在某处运行并返回结果的代码。AI 技能则为模型提供知识和流程:如何处理任务、遵循哪些步骤,以及怎样的输出才算优质。技能采用纯 Markdown 格式,因此不需要服务器、API 密钥或运行时环境,并且适用于任何模型。

查看全部

立即创建您自己的 AI 工作区

订阅我们的电子简报,获取新功能和后续赠奖活动的通知 👇