android?微信搶紅包工具AccessibilityService實現(xiàn)詳解
你有因為手速不夠快搶不到紅包而沮喪? 你有因為錯過紅包而懊惱嗎? 沒錯,它來了。。。
1、目標
使用AccessibilityService的方式,實現(xiàn)微信自動搶紅包(吐槽一下,網(wǎng)上找了許多文檔,由于各種原因,無法實現(xiàn)對應效果,所以先給自己整理下),關(guān)于AccessibilityService的文章,網(wǎng)上有很多(沒錯,多的都懶得貼鏈接那種多),可自行查找。
2、實現(xiàn)流程
1、流程分析(這里只分析在桌面的情況)
我們把一個搶紅包發(fā)的過程拆分來看,可以分為幾個步驟
收到通知 -> 點擊通知欄 -> 點擊紅包 -> 點擊開紅包 -> 退出紅包詳情頁
以上是一個搶紅包的基本流程。
2、實現(xiàn)步驟
1、收到通知 以及 點擊通知欄
接收通知欄的消息,介紹兩種方式
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() } } } } } }
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、點擊紅包
通過上述的跳轉(zhuǎn),可以進入聊天詳情頁面,到達詳情頁之后,接下來就是點擊對應的紅包卡片,那么問題來了,怎么點?肯定不是手動點。。。
我們來分析一下,一個聊天列表中,我們怎樣才能識別到紅包卡片,我看網(wǎng)上有通過findAccessibilityNodeInfosByViewId來獲取對應的View,這個也可以,只是我們獲取id的方式需要借助工具,可以用Android Device Monitor,但是這玩意早就廢廢棄了,雖然在sdk的目錄下存在monitor,奈何本人太菜,點擊就是打不開
我本地的jdk是11,我懷疑是不兼容,畢竟Android Device Monitor太老了。換新的layout Inspector,也就看看本地的debug應用,無法查看微信的呀。要么就反編譯,這個就先不考慮了,換findAccessibilityNodeInfosByText這個方法試試。
這個方法從字面意思能看出來,是通過text來匹配的,我們可以知道紅包卡片上面是有“微信紅包”的固定字樣的,是不是可以通股票這個來匹配呢,這還有個其他問題,并不是所有的紅包都需要點,比如已過期,已領取的是不是要過濾下,咋一看挺好過濾的,一個循環(huán)就好,仔細想,這是棵樹,不太好剔除,所以換了個思路。
最終方案就是遞歸一棵樹,往一個列表里面塞值,“已過期”和“已領取”的塞一個字符串“#”,匹配到“微信紅包”的塞一個AccessibilityNodeInfo,這樣如果這個紅包不能搶,那肯定一前一后分別是一個字符串和一個AccessibilityNodeInfo,因此,我們讀到一個AccessibilityNodeInfo,并且前一個值不是字符串,就可以執(zhí)行點擊事件,代碼如下
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() || "已被領完" == 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的前提下,我們也不知道則呢么獲取它,所以采用迂回套路,找固定的東西,我這里發(fā)現(xiàn)每個開紅包的頁面都有個“xxx的紅包”文案,然后這個頁面比較簡單,只有個關(guān)閉,和開紅包,我們通過獲取“xxx的紅包”對應的View來獲取父View,然后遞歸子View,判斷可點擊的,執(zhí)行點擊事件不就可以了嗎
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,來獲取父布局,然后遞歸子布局,依次執(zhí)行點擊事件,當然關(guān)閉事件是在前面的,也就是說關(guān)閉會優(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 && child.isClickable) { child.performAction(AccessibilityNodeInfo.ACTION_CLICK) } } } } } }
3、遇到問題
1、AccessibilityService收不到AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED事件
android碎片問題很正常,我這邊是使用NotificationListenerService來替代的。
2、需要點擊View的定位
簡單是就是到頁面應該點哪個View,找到相應的規(guī)則,來過濾出對應的View,這個規(guī)則是隨著微信的改變而變化的,findAccessibilityNodeInfosByViewId最直接,但是奈何工具問題,有點麻煩,于是采用取巧的辦法,通過找到其他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 & $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 -> handleNotification(event) AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED -> { val className = event.className.toString() Log.e("測試無障礙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() } } } } /** * 處理通知欄信息 * * 如果是微信紅包的提示信息,則模擬點擊 * * @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() } } } } } } /** * 關(guān)閉紅包詳情界面,實現(xiàn)自動返回聊天窗口 */ @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() { 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) } } } } } } /** * 模擬點擊,打開搶紅包界面 */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 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 } } } } /** * 遞歸查找當前聊天窗口中的紅包信息 * * 聊天窗口中的紅包都存在"領取紅包"一詞,因此可根據(jù)該詞查找紅包 * * @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、總結(jié)
此文是對AccessibilityService的使用的一個梳理,這個功能其實不麻煩,主要是一些細節(jié)問題,像自動領取支付寶紅包,自動領取QQ紅包或者其他功能等也都可以用類似方法實現(xiàn)。
以上就是android 微信搶紅包工具AccessibilityService實現(xiàn)詳解的詳細內(nèi)容,更多關(guān)于android AccessibilityService的資料請關(guān)注腳本之家其它相關(guān)文章!
- Android?Service啟動綁定流程詳解
- Android布局控件View?ViewRootImpl?WindowManagerService關(guān)系
- Android?O對后臺Service限制詳解
- Android?NotificationListenerService通知監(jiān)聽服務使用
- Android?NotificationListenerService?通知服務原理解析
- Android開發(fā)InputManagerService創(chuàng)建與啟動流程
- Android 10 啟動之servicemanager源碼解析
- Android 開機自啟動Service實現(xiàn)詳解
相關(guān)文章
android 點擊EditText始終不彈出軟件鍵盤實現(xiàn)代碼
這篇文章主要介紹了android 點擊EditText始終不彈出軟件鍵盤實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11詳解Android Automotive車載應用對駕駛模式Safe Drive Mode的適配
這篇文章主要介紹了詳解Android Automotive車載應用對駕駛模式(Safe Drive Mode)的適配,對車載應用感興趣的同學可以參考下2021-04-04Android?Bugreport實現(xiàn)原理深入分析
這篇文章主要介紹了Android?Bugreport實現(xiàn)原理,Bugreport主要用于分析手機的狀態(tài),在應用開發(fā)中,程序的調(diào)試分析是日常生產(chǎn)中進程會進行的工作,Bugreport就是很常用的工具,需要的朋友可以參考下2024-05-05