AccessibilityService新增gesturedescription使用詳解,7.0模擬手勢搶紅包核心代碼分析

目前市面上大部分搶微信搶紅包外掛的都失效了,最關鍵的原因就是在7.0我們再通過findAccessibilityNodeInfosByText或者findAccessibilityNodeInfosByViewId來拿View已經不行了,主要是系統已經換了新的方式實現,這就是本次要分享的內容,雖然我們拿不到View,但是我們可以通過模擬手勢,一樣可以實現點擊拆紅包
下邊就開始一步一步解析
首先是dispatchGesture方法的解釋

boolean dispatchGesture (GestureDescription gesture, 
                AccessibilityService.GestureResultCallback callback, 
                Handler handler)

這個就是我們要實現模擬手勢需要調用的方法
這個方法有三個參數:

  1. 參數GestureDescription:翻譯過來就是手勢的描述,如果要實現模擬,首先要描述你的腰模擬的手勢嘛
  2. 參數GestureResultCallback:翻譯過來就是手勢的回調,手勢模擬執行以後回調結果
  3. 參數handler:大部分情況我們不用的話傳空就可以了
    一般我們關注GestureDescription這個參數就夠了,下邊就重點介紹一下這個參數

GestureDescription官網描述:

Accessibility services with the AccessibilityService_canPerformGestures property can dispatch gestures. This class describes those gestures. Gestures are made up of one or more strokes. Gestures are immutable once built. 
Spatial dimensions throughout are in screen pixels. Time is measured in milliseconds.

翻譯過來大概意思:

輔助功能服務,其具有AccessibilityService_canPerformGestures屬性,可發送手勢。此類描述手勢。手勢由一個或多個筆劃組成。一旦生成, 手勢是不可改變的。
整個空間尺寸都以屏幕像素爲單位。時間以毫秒爲單位。

構建一個手勢描述的關鍵代碼:

GestureDescription.StrokeDescription(Path path, long startTime, long duration)

例如:

val builder = GestureDescription.Builder()
val gestureDescription = builder
                    .addStroke(GestureDescription.StrokeDescription(path, 100, 50))
                    .build()

參數介紹如下:

  • 參數path:筆畫路徑
  • 參數startTime:時間 (以毫秒爲單位),從手勢開始到開始筆劃的時間,非負數
  • 參數duration:筆劃經過路徑的持續時間(以毫秒爲單位),非負數

這裏重點介紹path,官網描述:

ath: The path to follow. Must have exactly one contour. The bounds of the path must not be negative. The path must not be empty. If the path has zero length (for example, a single moveTo()), the stroke is a touch that doesn't move.
This value must never be null.

翻譯過來大概意思就是:

Path: 要跟隨的路徑。必須有正好一個輪廓。路徑的邊界不得爲負數。路徑不得爲空。如果路徑的長度爲零 (例如, 單個moveTo()), 則筆劃是一個沒有移動的點擊

那就很清晰了,如果要模擬單機事件,直接像這樣就好:

val path = Path()
path.moveTo(x, y)

注意:其中x和y表示要點擊的按鈕座標相對於屏幕左上角的座標
那如果要模擬滾動手勢就可以像這樣:

val path = Path()
path.moveTo(x1, y1)
path.lineTo(x2, y2)

表示模擬從第一個點滾動到第二個點,其他更復雜的手勢都可以通過path模擬
至此關於模擬手勢問題介紹完畢,如有疑問可留言

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章