你有由于手速不够快抢不到红包而沮丧? 你有由于错失红包而沮丧吗? 没错,它来了。。。

一、目标

运用AccessibilityService的办法,完成微信主动抢红包(吐槽一下,网上找了许多文档,由于各种原因,无法完成对应作用,所以先给自己整理下),关于AccessibilityService的文章,网上有许多(没错,多的都懒得贴链接那种多),可自行查找。

二、完成流程

1、流程剖析(这儿只剖析在桌面的情况)

咱们把一个抢红包发的过程拆分来看,能够分为几个过程:

收到告诉 -> 点击告诉栏 -> 点击红包 -> 点击开红包 -> 退出红包概况页

以上是一个抢红包的根本流程。

2、完成过程

1、收到告诉 以及 点击告诉栏

接纳告诉栏的音讯,介绍两种办法

Ⅰ、AccessibilityService

即经过AccessibilityService的AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED事情来获取到Notification

private fun handleNotification(event: AccessibilityEvent) {
    val texts = event.text
    if (!texts.isEmpty()) {
            for (text in texts) {
                val content = text.toString()
                //假如微信红包的提示信息,则模仿点击进入相应的谈天窗口
                if (content.contains("[微信红包]")) {
                    if (event.parcelableData != null && event.parcelableData is Notification) {
                        val notification: Notification? = event.parcelableData as Notification?
                        val pendingIntent: PendingIntent = notification!!.contentIntent
                        try {
                            pendingIntent.send()
                        } catch (e: CanceledException) {
                            e.printStackTrace()
                        }
                    }
                }
            }
        }
}
Ⅱ、NotificationListenerService

这是监听告诉栏的另一种办法,记住要获取权限哦

class MyNotificationListenerService : NotificationListenerService() {
    override fun onNotificationPosted(sbn: StatusBarNotification?) {
        super.onNotificationPosted(sbn)
        val extras = sbn?.notification?.extras
        // 获取接纳音讯APP的包名
        val notificationPkg = sbn?.packageName
        // 获取接纳音讯的昂首
        val notificationTitle = extras?.getString(Notification.EXTRA_TITLE)
        // 获取接纳音讯的内容
        val notificationText = extras?.getString(Notification.EXTRA_TEXT)
        if (notificationPkg != null) {
            Log.d("收到的音讯内容包名:", notificationPkg)
            if (notificationPkg == "com.tencent.mm"){
                if (notificationText?.contains("[微信红包]") == true){
                    //收到微信红包了
                    val intent = sbn.notification.contentIntent
                    intent.send()
                }
            }
        }
        Log.d("收到的音讯内容", "Notification posted $notificationTitle & $notificationText")
    }
    override fun onNotificationRemoved(sbn: StatusBarNotification?) {
        super.onNotificationRemoved(sbn)
    }
}

2、点击红包

经过上述的跳转,能够进入谈天概况页面,抵达概况页之后,接下来便是点击对应的红包卡片,那么问题来了,怎样点?必定不是手动点。。。

咱们来剖析一下,一个谈天列表中,咱们怎样才能识别到红包卡片,我看网上有经过findAccessibilityNodeInfosByViewId来获取对应的View,这个也能够,只是咱们获取id的办法需求凭借东西,能够用Android Device Monitor,可是这玩意早就废废弃了,虽然在sdk的目录下存在monitor,怎么办本人太菜,点击便是打不开

android 微信抢红包工具 AccessibilityService

我本地的jdk是11,我怀疑是不兼容,究竟Android Device Monitor太老了。换新的layout Inspector,也就看看本地的debug应用,无法查看微信的呀。要么就反编译,这个就先不考虑了,或许在装备文件中设置android:accessibilityFlags=”flagReportViewIds”,然后暴力遍历Node树,打印相应的viewId和className,找到目标id即可。当然也能够换findAccessibilityNodeInfosByText这个办法试试。

这个办法从字面意思能看出来,是经过text来匹配的,咱们能够知道红包卡片上面是有“微信红包”的固定字样的,是不是能够通股票这个来匹配呢,这还有个其他问题,并不是一切的红包都需求点,比方已过期,已收取的是不是要过滤下,咋一看挺好过滤的,一个循环就好,仔细想,这是棵树,不太好除掉,所以换了个思路。

终究方案便是递归一棵树,往一个列表里边塞值,“已过期”和“已收取”的塞一个字符串“#”,匹配到“微信红包”的塞一个AccessibilityNodeInfo,这样假如这个红包不能抢,那必定一前一后分别是一个字符串和一个AccessibilityNodeInfo,因此,咱们读到一个AccessibilityNodeInfo,并且前一个值不是字符串,就能够履行点击事情,代码如下

private fun getPacket() {
    val rootNode = rootInActiveWindow
    val caches:ArrayList<Any> = ArrayList()
    recycle(rootNode,caches)
    if(caches.isNotEmpty()){
        for(index in 0 until caches.size){
            if(caches[index] is AccessibilityNodeInfo && (index == 0 || caches[index-1] !is String )){
                val node = caches[index] as AccessibilityNodeInfo
                var parent = node.parent
                while (parent != null) {
                    if (parent.isClickable) {
                        parent.performAction(AccessibilityNodeInfo.ACTION_CLICK)
                        break
                    }
                    parent = parent.parent
                }
                break
            }
        }
    }
}
private fun recycle(node: AccessibilityNodeInfo,caches:ArrayList<Any>) {
        if (node.childCount == 0) {
            if (node.text != null) {
                if ("已过期" == node.text.toString() || "已被领完" == node.text.toString() || "已收取" == node.text.toString()) {
                    caches.add("#")
                }
                if ("微信红包" == node.text.toString()) {
                    caches.add(node)
                }
            }
        } else {
            for (i in 0 until node.childCount) {
                if (node.getChild(i) != null) {
                    recycle(node.getChild(i),caches)
                }
            }
        }
    }

以上只点击了第一个能点击的红包卡片,想点击一切的可另行处理。

3、点击开红包

这儿思路跟上面相似,开红包页面比较简单,可是怎么办开红包是个按钮,在不知道id的前提下,咱们也不知道则呢么获取它,所以选用迂回套路,找固定的东西,我这儿发现每个开红包的页面都有个“xxx的红包”案牍,然后这个页面比较简单,只有个封闭,和开红包,咱们经过获取“xxx的红包”对应的View来获取父View,然后递归子View,判别可点击的,履行点击事情不就能够了吗

private fun openPacket() {
    val nodeInfo = rootInActiveWindow
    if (nodeInfo != null) {
        val list = nodeInfo.findAccessibilityNodeInfosByText ("的红包")
        for ( i in 0 until list.size) {
            val parent = list[i].parent
            if (parent != null) {
                for ( j in 0 until  parent.childCount) {
                    val child = parent.getChild (j)
                    if (child != null && child.isClickable) {
                        child.performAction(AccessibilityNodeInfo.ACTION_CLICK)
                    }
                }
            }
        }
    }
}

4、退出红包概况页

这儿回退也是个按钮,咱们也不知道id,所以能够跟点开红包相同,迂回套路,获取其他的View,来获取父布局,然后递归子布局,依次履行点击事情,当然封闭事情是在前面的,也便是说封闭会优先履行到

private fun close() {
    val nodeInfo = rootInActiveWindow
    if (nodeInfo != null) {
        val list = nodeInfo.findAccessibilityNodeInfosByText ("的红包")
        if (list.isNotEmpty()) {
            val parent = list[0].parent.parent.parent
            if (parent != null) {
                for ( j in 0 until  parent.childCount) {
                    val child = parent.getChild (j)
                    if (child != null && child.isClickable) {
                        child.performAction(AccessibilityNodeInfo.ACTION_CLICK)
                    }
                }
            }
        }
    }
}

三、遇到问题

1、AccessibilityService收不到AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED事情

android碎片问题很正常,我这边是运用NotificationListenerService来代替的。

2、需求点击View的定位

简单是便是到页面应该点哪个View,找到相应的规矩,来过滤出对应的View,这个规矩是跟着微信的改变而变化的,findAccessibilityNodeInfosByViewId最直接,可是怎么办东西问题,有点麻烦,遍历打印能够获取,可是id每个版本或许会变。还有便是经过案牍来获取,即findAccessibilityNodeInfosByText,获取一些固定案牍的View,这个相对而言在不改版,或许不会变,相对稳定些,假如这个案牍的View本身没点击事情,可获取它的parent,尝试点击,或许遍历parent树,根据isClickable来判别是否能够点击。

划重点:

这儿还有一种便是钉钉的开红包按钮,折腾了半天,一向拿不到,各种递归遍历,一向没有找到,最后换了个办法,经过AccessibilityService的模仿点击来做,也便是经过坐标来模仿点击,当然要在装备中开启android:canPerformGestures=”true”, 然后经过 accessibilityService.dispatchGesture() 来处理,详细坐标能够拿一个其他的View,然后经过份额来确定大概得方位,或许,看看能不能拿到外层的Layout也是相同的

object AccessibilityClick {
    fun click(accessibilityService: AccessibilityService, x: Float, y: Float) {
        val builder = GestureDescription.Builder()
        val path = Path()
        path.moveTo(x, y)
        path.lineTo(x, y)
        builder.addStroke(GestureDescription.StrokeDescription(path, 0, 10))
        accessibilityService.dispatchGesture(builder.build(), object : AccessibilityService.GestureResultCallback() {
            override fun onCancelled(gestureDescription: GestureDescription) {
                super.onCancelled(gestureDescription)
            }
            override fun onCompleted(gestureDescription: GestureDescription) {
                super.onCompleted(gestureDescription)
            }
        }, null)
    }
}

四、完整代码

MyNotificationListenerService

class MyNotificationListenerService : NotificationListenerService() {
    override fun onNotificationPosted(sbn: StatusBarNotification?) {
        super.onNotificationPosted(sbn)
        val extras = sbn?.notification?.extras
        // 获取接纳音讯APP的包名
        val notificationPkg = sbn?.packageName
        // 获取接纳音讯的昂首
        val notificationTitle = extras?.getString(Notification.EXTRA_TITLE)
        // 获取接纳音讯的内容
        val notificationText = extras?.getString(Notification.EXTRA_TEXT)
        if (notificationPkg != null) {
            Log.d("收到的音讯内容包名:", notificationPkg)
            if (notificationPkg == "com.tencent.mm"){
                if (notificationText?.contains("[微信红包]") == true){
                    //收到微信红包了
                    val intent = sbn.notification.contentIntent
                    intent.send()
                }
            }
        } Log.d("收到的音讯内容", "Notification posted $notificationTitle & $notificationText")
    }
    override fun onNotificationRemoved(sbn: StatusBarNotification?) {
        super.onNotificationRemoved(sbn)
    }
}

MyAccessibilityService

class MyAccessibilityService : AccessibilityService() {
    override fun onAccessibilityEvent(event: AccessibilityEvent) {
        val eventType = event.eventType
        when (eventType) {
            AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED -> handleNotification(event)
            AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED -> {
                val className = event.className.toString()
                Log.e("测验无障碍",className)
                when (className) {
                    "com.tencent.mm.ui.LauncherUI" -> {
                        // 我管这叫红包卡片页面
                        getPacket()
                    }
                    "com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI" -> {
                        // 貌似是老UI debug没发现进来
                        openPacket()
                    }
                    "com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyNotHookReceiveUI" -> {
                        // 应该是红包弹框UI新页面 debug进来了
                        openPacket()
                    }
                    "com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI" -> {
                        // 红包概况页面  履行封闭操作
                        close()
                    }
                    "androidx.recyclerview.widget.RecyclerView" -> {
                        // 这个比较频繁  主要是在谈天页面  有红包来的时候  会触发  当然其他有列表的页面也或许触发  没想到好的过滤办法
                        getPacket()
                    }
                }
            }
        }
    }
    /**
     * 处理告诉栏信息
     *
     * 假如是微信红包的提示信息,则模仿点击
     *
     * @param event
     */
    private fun handleNotification(event: AccessibilityEvent) {
        val texts = event.text
        if (!texts.isEmpty()) {
            for (text in texts) {
                val content = text.toString()
                //假如微信红包的提示信息,则模仿点击进入相应的谈天窗口
                if (content.contains("[微信红包]")) {
                    if (event.parcelableData != null && event.parcelableData is Notification) {
                        val notification: Notification? = event.parcelableData as Notification?
                        val pendingIntent: PendingIntent = notification!!.contentIntent
                        try {
                            pendingIntent.send()
                        } catch (e: CanceledException) {
                            e.printStackTrace()
                        }
                    }
                }
            }
        }
    }
    /**
     * 封闭红包概况界面,完成主动返回谈天窗口
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    private fun close() {
        val nodeInfo = rootInActiveWindow
        if (nodeInfo != null) {
            val list = nodeInfo.findAccessibilityNodeInfosByText ("的红包")
            if (list.isNotEmpty()) {
                val parent = list[0].parent.parent.parent
                if (parent != null) {
                    for ( j in 0 until  parent.childCount) {
                        val child = parent.getChild (j)
                        if (child != null && child.isClickable) {
                            child.performAction(AccessibilityNodeInfo.ACTION_CLICK)
                        }
                    }
                }
            }
        }
    }
    /**
     * 模仿点击,拆开红包
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    private fun openPacket() {
        Log.e("测验无障碍","点击红包")
        Thread.sleep(100)
        val nodeInfo = rootInActiveWindow
        if (nodeInfo != null) {
            val list = nodeInfo.findAccessibilityNodeInfosByText ("的红包")
            for ( i in 0 until list.size) {
                val parent = list[i].parent
                if (parent != null) {
                    for ( j in 0 until  parent.childCount) {
                        val child = parent.getChild (j)
                        if (child != null && child.isClickable) {
                            Log.e("测验无障碍","点击红包成功")
                            child.performAction(AccessibilityNodeInfo.ACTION_CLICK)
                        }
                }
            }
            }
        }
    }
    /**
     * 模仿点击,翻开抢红包界面
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private fun getPacket() {
        Log.e("测验无障碍","获取红包")
        val rootNode = rootInActiveWindow
        val caches:ArrayList<Any> = ArrayList()
        recycle(rootNode,caches)
        if(caches.isNotEmpty()){
            for(index in 0 until caches.size){
                if(caches[index] is AccessibilityNodeInfo && (index == 0 || caches[index-1] !is String )){
                    val node = caches[index] as AccessibilityNodeInfo
//                    node.performAction(AccessibilityNodeInfo.ACTION_CLICK)
                    var parent = node.parent
                    while (parent != null) {
                        if (parent.isClickable) {
                            parent.performAction(AccessibilityNodeInfo.ACTION_CLICK)
                            Log.e("测验无障碍","获取红包成功")
                            break
                        }
                        parent = parent.parent
                    }
                    break
                }
            }
        }
    }
    /**
     * 递归查找当前谈天窗口中的红包信息
     *
     * 谈天窗口中的红包都存在"微信红包"一词,因此可根据该词查找红包
     *
     * @param node
     */
    private fun recycle(node: AccessibilityNodeInfo,caches:ArrayList<Any>) {
        if (node.childCount == 0) {
            if (node.text != null) {
                if ("已过期" == node.text.toString() || "已被领完" == node.text.toString() || "已收取" == node.text.toString()) {
                    caches.add("#")
                }
                if ("微信红包" == node.text.toString()) {
                    caches.add(node)
                }
            }
        } else {
            for (i in 0 until node.childCount) {
                if (node.getChild(i) != null) {
                    recycle(node.getChild(i),caches)
                }
            }
        }
    }
    override fun onInterrupt() {}
    override fun onServiceConnected() {
        super.onServiceConnected()
        Log.e("测验无障碍id","启动")
        val info: AccessibilityServiceInfo = serviceInfo
        info.packageNames = arrayOf("com.tencent.mm")
        serviceInfo = info
    }
}

5、总结

此文是对AccessibilityService的运用的一个梳理,这个功用其实不麻烦,主要是一些细节问题,像主动收取支付宝红包,主动收取QQ红包或许其他功用等也都能够用相似办法完成。目前完成了微信和钉钉的,剩余的支付宝QQ啥的没啥人用,就不想做了,不过原理都是相同的,

源码地址: gitee.com/wlr123/acce…

运用时记住开启下对应权限,设置下后台运转权限,电量设置里边允许后台运转等,以及告诉栏权限,以确保稳定运转