Mobile Development 14 min read

How to Build an Android Plugin that Auto‑Clicks WeChat Red Packets

This tutorial explains how to create an Android AccessibilityService plugin that automatically detects and clicks all WeChat red packets, detailing its stage‑based logic, screen‑content detection, node handling, queue management, and provides complete source code for educational use.

21CTO
21CTO
21CTO
How to Build an Android Plugin that Auto‑Clicks WeChat Red Packets

Expected Features

Capture and click every red packet displayed on the screen, unlike other plugins that only handle the newest one.

Intelligently skip already‑clicked packets to avoid unnecessary interactions.

Optional red‑packet log for reviewing captured packets.

Performance optimizations so the plugin runs invisibly in the background.

Well‑documented teaching code suitable for reading and learning.

Implementation Principle

1. Red Packet Process Logic Control

The plugin uses a Stage singleton to record the current processing stage. It exposes constants for each stage and methods to enter a stage and retrieve the current stage.

public class Stage {
    private static Stage stageInstance;
    public static final int FETCHING_STAGE = 0, OPENING_STAGE = 1, FETCHED_STAGE = 2, OPENED_STAGE = 3;
    private int currentStage = FETCHED_STAGE;
    private Stage() {}
    public static Stage getInstance() {
        if (stageInstance == null) stageInstance = new Stage();
        return stageInstance;
    }
    public void entering(int _stage) {
        stageInstance.currentStage = _stage;
    }
    public int getCurrentStage() {
        return stageInstance.currentStage;
    }
}

1.1 Stage Description

FETCHING_STAGE : Reading red packets on the screen; no other actions should occur.

FETCHED_STAGE : Finished a fetch cycle; all detected packets are added to the queue.

OPENING_STAGE : Opening a packet; other actions are blocked.

OPENED_STAGE : Packet successfully opened and the detail page is displayed.

The workflow starts at FETCHED_STAGE, adds packets to the queue, then repeatedly processes the queue:

--> FETCHED_STAGE --> FETCHING_STAGE --> FETCHED_STAGE -->

Processing each packet:

--> [CLICK] --> OPENING_STAGE --> [CLICK] --> OPENED_STAGE --> [BACK] --> FETCHED_STAGE --> (success)

--> [CLICK] --> OPENING_STAGE --> [BACK] --> FETCHED_STAGE --> (failure)

The loop repeats steps 1 and 2 continuously.

2. Screen Content Detection and Automated Click

The plugin relies on Android's AccessibilityService to monitor UI changes and perform clicks.

2.1 Configure AccessibilityService

Only events related to window state or content changes are needed, and the target package is WeChat ( com.tencent.mm).

android:accessibilityEventTypes="typeWindowStateChanged|typeWindowContentChanged"
android:packageNames="com.tencent.mm"
android:canRetrieveWindowContent="true"

2.2 Obtain Red‑Packet Nodes

Retrieve the root node of the active window either via event.getSource() or getRootInActiveWindow(). The text "领取红包" (receive red packet) is used as the identifier.

List<AccessibilityNodeInfo> node1 = nodeInfo.findAccessibilityNodeInfosByText("领取红包");

2.3 Operate on Nodes

Click actions are performed with performAction. Because the clickable attribute is often on the parent node, the plugin climbs to the parent before clicking.

nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
performGlobalAction(GLOBAL_ACTION_BACK);

3. Collect All Red Packets on Screen

The plugin gathers every red‑packet node, filters out those already processed, and adds the rest to a queue.

3.1 Identify Already‑Handled Packets

Instance IDs alone are unreliable due to object pooling. The plugin creates a hash from the packet content plus the node instance ID to uniquely identify a packet.

3.2 Add New Packets to the Queue

private List<AccessibilityNodeInfo> nodesToFetch = new ArrayList<>();
private List<String> fetchedIdentifiers = new ArrayList<>();

for (AccessibilityNodeInfo cellNode : fetchNodes) {
    String id = getHongbaoHash(cellNode);
    if (id != null && !fetchedIdentifiers.contains(id)) {
        nodesToFetch.add(cellNode);
    }
}

4. Open Packets from the Queue

4.1 Detect Node Reuse

Before clicking, the plugin checks whether the node has been recycled. If the hash returns null, the node is discarded and will be re‑queued later.

AccessibilityNodeInfo node = nodesToFetch.remove(nodesToFetch.size() - 1);
if (node.getParent() != null) {
    String id = getHongbaoHash(node);
    if (id == null) return;
    fetchedIdentifiers.add(id);
    Stage.getInstance().entering(Stage.OPENING_STAGE);
    node.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
}

4.2 Choose Action Based on Packet Type

After opening, the plugin looks for specific texts:

"拆红包" – packet is still available; click to open.

"过期" – packet expired.

"手慢了" – packet already taken.

"红包详情" – you have already claimed it.

The appropriate stage is set accordingly.

4.3 Prevent Premature Back Navigation

A loading animation may trigger events before the packet details appear. The plugin uses a TTL counter; if the maximum attempts are reached without the expected text, the packet is abandoned.

Stage.getInstance().entering(Stage.OPENING_STAGE);
ttl += 1;
return -1;

License and Usage

The source originates from a demo shown at Xiaomi's autumn conference. The stable branch builds on that code, while the dev branch rewrites most logic. The package name remains com.miui.hongbao.

This code is provided for teaching purposes only and must not be modified for other uses. It is released under the MIT License, allowing any use provided the license terms are respected.

WeChat Lucky Money Plugin
WeChat Lucky Money Plugin
QR Code
QR Code
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AndroidWeChatAccessibilityServicered packet
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.