Android 輔助功能 -搶紅包(二)

夏沫琅琊發表於2024-03-15

Android 輔助功能 -搶紅包(二)

本篇文章繼續講述輔助功能實現搶紅包的方案.

上篇文章主要講了下輔助功能的基本使用,本文涉及的一些基礎內容就不再贅述了.

有疑問的可以檢視上篇文章:

Android 輔助功能 -搶紅包

1: 新增微信監聽

修改xml檔案,android:packageNames中新增微信監聽.

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFeedbackType="feedbackAllMask"
    android:accessibilityFlags="flagDefault"
    android:canRetrieveWindowContent="true"
    android:canRequestFilterKeyEvents="true"
    android:description="@string/desc"
    android:packageNames= "com.test.redpacket,com.tencent.mm">
</accessibility-service>

2: 監聽通知欄訊息

public class RedPacketAccessibilityService extends AccessibilityService {
    private static final String TAG = "RedPacketAccessibilityService";

    @SuppressLint("LongLogTag")
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        int action = event.getAction();
        Log.i(TAG, "onAccessibilityEvent: " + action);
        CharSequence packageName = event.getPackageName();
        Log.i(TAG, "onAccessibilityEvent: " + packageName);

        AccessibilityNodeInfo rootInActiveWindow = getRootInActiveWindow();
        if (rootInActiveWindow != null) {
            List<AccessibilityNodeInfo> nodeInfos = rootInActiveWindow.findAccessibilityNodeInfosByText("紅包:恭喜發財");
            if (nodeInfos != null) {
                for (AccessibilityNodeInfo nodeInfo : nodeInfos) {
                    nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }
            }
        }

        switch (event.getEventType()) {
            case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
                List<CharSequence> text = event.getText();
                for (CharSequence charSequence : text) {
                    Log.i(TAG, "onAccessibilityEvent: notification ->" + charSequence);
                    if (charSequence.toString().contains("請求新增")) {
                        Parcelable parcelableData = event.getParcelableData();
                        if (parcelableData != null && parcelableData instanceof Notification) {
                            PendingIntent contentIntent = ((Notification) parcelableData).contentIntent;
                            new Handler().postDelayed(() -> {
                                try {
                                    contentIntent.send();
                                } catch (PendingIntent.CanceledException e) {
                                    Log.e(TAG, "onAccessibilityEvent: ", e);
                                }
                            },1000);
                        }
                    }
                }
                break;
        }

    }

    @Override
    public void onInterrupt() {

    }
}

3: 測試

2024-03-15 12:58:49.361 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: com.tencent.mm
2024-03-15 12:58:49.386 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: notification ->夏沫: [微信紅包] 恭喜發財,大吉大利
2024-03-15 12:58:50.716 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: 0
2024-03-15 12:58:50.716 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: com.tencent.mm
2024-03-15 12:58:50.874 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: 0
2024-03-15 12:58:50.874 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: com.tencent.mm
2024-03-15 12:58:50.903 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: 0

如上述日誌輸出,監聽通知欄訊息後,延遲1s 跳轉到了聊天介面.

2024-03-15 12:59:04.633 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: 0
2024-03-15 12:59:04.633 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: com.tencent.mm
2024-03-15 12:59:04.640 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: notification ->夏沫請求新增你為朋友
2024-03-15 12:59:05.809 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: 0
2024-03-15 12:59:05.809 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: com.tencent.mm
2024-03-15 12:59:05.831 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: 0

如上述輸出,監聽通知欄新增好友訊息,延遲1s跳轉到了新的朋友介面.

本文由部落格一文多發平臺 OpenWrite 釋出!

相關文章