Use longarm and an AI Agent to Build a Safer Android Game Batch Task
- Metaphor Projects
- Android Automation
- 12 Aug, 2026
Many mobile games have a simple repeat loop: start a round, let it play, collect its result, and begin again. When a game permits this kind of automation, longarm can make the loop repeatable on an Android phone without giving a remote service access to the screen.
This article uses a generic tower-defense game as an example. The goal is not to bypass game rules, ads, purchases, rate limits, or anti-cheat protections. Check the game’s terms and use automation only where it is allowed.

Split the Work Between an AI Agent and longarm
The AI agent is best at setup and verification. It can inspect screenshots, identify the right screen regions, test one action at a time, and build a batch task from the verified actions.
longarm is best at execution. Once the task is saved, it opens the app, performs gestures, waits for a visual state, and repeats the loop locally on the device.
This separation matters. A long-running task should not blindly replay guesses made from a single screenshot.
Start With One Action at a Time
Before creating a batch task, have the agent test each action independently:
- Open the game package and confirm the expected home screen.
- Capture a screenshot with a coordinate grid.
- Tap the Start or Battle button and confirm that a live round begins.
- End or wait for one test round, capture the result screen, and identify the Retry button.
- Tap Retry once and confirm that the next round begins.
This catches two common problems: a screenshot may be scaled in a viewer, and a Unity-based game may not expose its controls through Android Accessibility.
Use OCR for the End-of-Round Condition
Accessibility selectors are ideal for normal Android apps. Games made with Unity or another custom renderer often draw their own text and controls, however, so the accessibility tree can be empty.
longarm 1.3.0 adds on-device OCR conditions. A batch can capture a cropped part of the current screen and wait until it recognizes a word such as RETRY. Crop tightly around the expected result button to reduce work and avoid matching an unrelated word.
For example, the task can wait for the result screen like this:
{
"type": "wait_for_ocr",
"params": {
"text": "RETRY",
"match": "contains",
"x": 50,
"y": 1450,
"width": 430,
"height": 400,
"pollIntervalMs": 120000,
"timeoutMs": 3600000
}
}
The task first waits for a minimum expected round duration, then checks that screen region every two minutes. This is usually much lighter on battery than checking continuously.
Assemble the Batch Loop
A dependable task has four phases:
- Open the game activity.
- Tap the verified Start or Battle coordinate.
- Wait for a minimum time, then wait for OCR to find the results control.
- Tap the verified Retry coordinate and repeat.
Use a fixed maximum loop count and a sensible OCR timeout. Both give the task a clear end rather than allowing an unattended loop to run forever.
Keep Control of the Task
longarm’s Stop button interrupts ordinary delays and OCR waits immediately. Test Stop with a short disposable task before leaving a longer run unattended.
Keep the device charged, keep Accessibility enabled, and make sure the game does not show a dialog that covers the result button. If the game changes its layout, redo the single-action checks before restarting the saved loop.
Why the AI Agent Still Helps
An AI agent does not need to be in the loop for every tap. Its value is in making the automation explainable and testable: it can inspect the screen, determine whether OCR is appropriate, select a crop, validate coordinates, and save a reusable longarm batch task.
That turns a fragile macro into a local workflow you can inspect, stop, adjust, and run again when appropriate.