欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android?微信搶紅包工具AccessibilityService實(shí)現(xiàn)詳解

 更新時(shí)間:2023年02月07日 09:45:24   作者:我有一頭小毛驢你有嗎  
這篇文章主要為大家介紹了android?微信搶紅包工具AccessibilityService實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

你有因?yàn)槭炙俨粔蚩鞊尣坏郊t包而沮喪? 你有因?yàn)殄e(cuò)過紅包而懊惱嗎? 沒錯(cuò),它來了。。。

1、目標(biāo)

使用AccessibilityService的方式,實(shí)現(xiàn)微信自動(dòng)搶紅包(吐槽一下,網(wǎng)上找了許多文檔,由于各種原因,無法實(shí)現(xiàn)對(duì)應(yīng)效果,所以先給自己整理下),關(guān)于AccessibilityService的文章,網(wǎng)上有很多(沒錯(cuò),多的都懶得貼鏈接那種多),可自行查找。

2、實(shí)現(xiàn)流程

1、流程分析(這里只分析在桌面的情況)

我們把一個(gè)搶紅包發(fā)的過程拆分來看,可以分為幾個(gè)步驟

收到通知 -> 點(diǎn)擊通知欄 -> 點(diǎn)擊紅包 -> 點(diǎn)擊開紅包 -> 退出紅包詳情頁

以上是一個(gè)搶紅包的基本流程。

2、實(shí)現(xiàn)步驟

1、收到通知 以及 點(diǎn)擊通知欄

接收通知欄的消息,介紹兩種方式

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()
                //如果微信紅包的提示信息,則模擬點(diǎn)擊進(jìn)入相應(yīng)的聊天窗口
                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()
                        }
                    }
                }
            }
        }
}
2、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)
        // 獲取接收消息的內(nèi)容
        val notificationText = extras?.getString(Notification.EXTRA_TEXT)
        if (notificationPkg != null) {
            Log.d("收到的消息內(nèi)容包名:", notificationPkg)
            if (notificationPkg == "com.tencent.mm"){
                if (notificationText?.contains("[微信紅包]") == true){
                    //收到微信紅包了
                    val intent = sbn.notification.contentIntent
                    intent.send()
                }
            }
        }
        Log.d("收到的消息內(nèi)容", "Notification posted $notificationTitle & $notificationText")
    }
    override fun onNotificationRemoved(sbn: StatusBarNotification?) {
        super.onNotificationRemoved(sbn)
    }
}

2、點(diǎn)擊紅包

通過上述的跳轉(zhuǎn),可以進(jìn)入聊天詳情頁面,到達(dá)詳情頁之后,接下來就是點(diǎn)擊對(duì)應(yīng)的紅包卡片,那么問題來了,怎么點(diǎn)?肯定不是手動(dòng)點(diǎn)。。。

我們來分析一下,一個(gè)聊天列表中,我們?cè)鯓硬拍茏R(shí)別到紅包卡片,我看網(wǎng)上有通過findAccessibilityNodeInfosByViewId來獲取對(duì)應(yīng)的View,這個(gè)也可以,只是我們獲取id的方式需要借助工具,可以用Android Device Monitor,但是這玩意早就廢廢棄了,雖然在sdk的目錄下存在monitor,奈何本人太菜,點(diǎn)擊就是打不開

我本地的jdk是11,我懷疑是不兼容,畢竟Android Device Monitor太老了。換新的layout Inspector,也就看看本地的debug應(yīng)用,無法查看微信的呀。要么就反編譯,這個(gè)就先不考慮了,換findAccessibilityNodeInfosByText這個(gè)方法試試。

這個(gè)方法從字面意思能看出來,是通過text來匹配的,我們可以知道紅包卡片上面是有“微信紅包”的固定字樣的,是不是可以通股票這個(gè)來匹配呢,這還有個(gè)其他問題,并不是所有的紅包都需要點(diǎn),比如已過期,已領(lǐng)取的是不是要過濾下,咋一看挺好過濾的,一個(gè)循環(huán)就好,仔細(xì)想,這是棵樹,不太好剔除,所以換了個(gè)思路。

最終方案就是遞歸一棵樹,往一個(gè)列表里面塞值,“已過期”和“已領(lǐng)取”的塞一個(gè)字符串“#”,匹配到“微信紅包”的塞一個(gè)AccessibilityNodeInfo,這樣如果這個(gè)紅包不能搶,那肯定一前一后分別是一個(gè)字符串和一個(gè)AccessibilityNodeInfo,因此,我們讀到一個(gè)AccessibilityNodeInfo,并且前一個(gè)值不是字符串,就可以執(zhí)行點(diǎn)擊事件,代碼如下

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
                node.performAction(AccessibilityNodeInfo.ACTION_CLICK)
                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() || "已被領(lǐng)完" == node.text.toString() || "已領(lǐng)取" == 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)
                }
            }
        }
    }

以上只點(diǎn)擊了第一個(gè)能點(diǎn)擊的紅包卡片,想點(diǎn)擊所有的可另行處理。

3、點(diǎn)擊開紅包

這里思路跟上面類似,開紅包頁面比較簡(jiǎn)單,但是奈何開紅包是個(gè)按鈕,在不知道id的前提下,我們也不知道則呢么獲取它,所以采用迂回套路,找固定的東西,我這里發(fā)現(xiàn)每個(gè)開紅包的頁面都有個(gè)“xxx的紅包”文案,然后這個(gè)頁面比較簡(jiǎn)單,只有個(gè)關(guān)閉,和開紅包,我們通過獲取“xxx的紅包”對(duì)應(yīng)的View來獲取父View,然后遞歸子View,判斷可點(diǎn)擊的,執(zhí)行點(diǎn)擊事件不就可以了嗎

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 &amp;&amp; child.isClickable) {
                        child.performAction(AccessibilityNodeInfo.ACTION_CLICK)
                    }
                }
            }
        }
    }
}

4、退出紅包詳情頁

這里回退也是個(gè)按鈕,我們也不知道id,所以可以跟點(diǎn)開紅包一樣,迂回套路,獲取其他的View,來獲取父布局,然后遞歸子布局,依次執(zhí)行點(diǎn)擊事件,當(dāng)然關(guān)閉事件是在前面的,也就是說關(guān)閉會(huì)優(yōu)先執(zhí)行到

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 &amp;&amp; child.isClickable) {
                        child.performAction(AccessibilityNodeInfo.ACTION_CLICK)
                    }
                }
            }
        }
    }
}

3、遇到問題

1、AccessibilityService收不到AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED事件

android碎片問題很正常,我這邊是使用NotificationListenerService來替代的。

2、需要點(diǎn)擊View的定位

簡(jiǎn)單是就是到頁面應(yīng)該點(diǎn)哪個(gè)View,找到相應(yīng)的規(guī)則,來過濾出對(duì)應(yīng)的View,這個(gè)規(guī)則是隨著微信的改變而變化的,findAccessibilityNodeInfosByViewId最直接,但是奈何工具問題,有點(diǎn)麻煩,于是采用取巧的辦法,通過找到其他View來定位自身

4、完整代碼

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)
        // 獲取接收消息的內(nèi)容
        val notificationText = extras?.getString(Notification.EXTRA_TEXT)
        if (notificationPkg != null) {
            Log.d("收到的消息內(nèi)容包名:", notificationPkg)
            if (notificationPkg == "com.tencent.mm"){
                if (notificationText?.contains("[微信紅包]") == true){
                    //收到微信紅包了
                    val intent = sbn.notification.contentIntent
                    intent.send()
                }
            }
        } Log.d("收到的消息內(nèi)容", "Notification posted $notificationTitle &amp; $notificationText")
    }
    override fun onNotificationRemoved(sbn: StatusBarNotification?) {
        super.onNotificationRemoved(sbn)
    }
}

MyAccessibilityService

class RobService : AccessibilityService() {
    override fun onAccessibilityEvent(event: AccessibilityEvent) {
        val eventType = event.eventType
        when (eventType) {
            AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED -&gt; handleNotification(event)
            AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED -&gt; {
                val className = event.className.toString()
                Log.e("測(cè)試無障礙id",className)
                if (className == "com.tencent.mm.ui.LauncherUI") {
                    getPacket()
                } else if (className == "com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI") {
                    openPacket()
                }  else if (className == "com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI") {
                    close()
                }
            }
        }
    }
    /**
* 處理通知欄信息
*
* 如果是微信紅包的提示信息,則模擬點(diǎn)擊
*
* @param event
*/
    private fun handleNotification(event: AccessibilityEvent) {
        val texts = event.text
        if (!texts.isEmpty()) {
            for (text in texts) {
                val content = text.toString()
                //如果微信紅包的提示信息,則模擬點(diǎn)擊進(jìn)入相應(yīng)的聊天窗口
                if (content.contains("[微信紅包]")) {
                    if (event.parcelableData != null &amp;&amp; event.parcelableData is Notification) {
                        val notification: Notification? = event.parcelableData as Notification?
                        val pendingIntent: PendingIntent = notification!!.contentIntent
                        try {
                            pendingIntent.send()
                        } catch (e: CanceledException) {
                            e.printStackTrace()
                        }
                    }
                }
            }
        }
    }
    /**
* 關(guān)閉紅包詳情界面,實(shí)現(xiàn)自動(dòng)返回聊天窗口
*/
    @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 &amp;&amp; child.isClickable) {
                            child.performAction(AccessibilityNodeInfo.ACTION_CLICK)
                        }
                    }
                }
            }
        }
    }
    /**
* 模擬點(diǎn)擊,拆開紅包
*/
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    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 &amp;&amp; child.isClickable) {
                            child.performAction(AccessibilityNodeInfo.ACTION_CLICK)
                        }
                    }
                }
            }
        }
    }
    /**
* 模擬點(diǎn)擊,打開搶紅包界面
*/
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private fun getPacket() {
        val rootNode = rootInActiveWindow
        val caches:ArrayList&lt;Any&gt; = ArrayList()
        recycle(rootNode,caches)
        if(caches.isNotEmpty()){
            for(index in 0 until caches.size){
                if(caches[index] is AccessibilityNodeInfo &amp;&amp; (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)
                            break
                        }
                        parent = parent.parent
                    }
                    break
                }
            }
        }
    }
    /**
* 遞歸查找當(dāng)前聊天窗口中的紅包信息
*
* 聊天窗口中的紅包都存在"領(lǐng)取紅包"一詞,因此可根據(jù)該詞查找紅包
*
* @param node
*/
    private fun recycle(node: AccessibilityNodeInfo,caches:ArrayList&lt;Any&gt;) {
        if (node.childCount == 0) {
            if (node.text != null) {
                if ("已過期" == node.text.toString() || "已被領(lǐng)完" == node.text.toString() || "已領(lǐng)取" == 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("測(cè)試無障礙id","啟動(dòng)")
        val info: AccessibilityServiceInfo = serviceInfo
        info.packageNames = arrayOf("com.tencent.mm")
        serviceInfo = info
    }
}

5、總結(jié)

此文是對(duì)AccessibilityService的使用的一個(gè)梳理,這個(gè)功能其實(shí)不麻煩,主要是一些細(xì)節(jié)問題,像自動(dòng)領(lǐng)取支付寶紅包,自動(dòng)領(lǐng)取QQ紅包或者其他功能等也都可以用類似方法實(shí)現(xiàn)。

以上就是android 微信搶紅包工具AccessibilityService實(shí)現(xiàn)詳解的詳細(xì)內(nèi)容,更多關(guān)于android AccessibilityService的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論