Android 辅助功能 -抢红包(二)

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 发布!

posted @ 2024-03-15 14:17  夏沫琅琊  阅读(47)  评论(0编辑  收藏  举报