主題:嘗試實作APP在后臺自動點擊螢屏任意位置的功能。
根據網友的一些方法嘗試使用AccessbilityService中的gestureDescription進行模擬手勢操作(只嘗試了單擊)。但是呼叫手勢存在例外,在回呼GestureResultCallback中總是走到onCancel而不是onComplete。不知道為何,從表象上看也沒有觸發我想要的單擊效果。
關鍵代碼如下:
fun click(x: Int, y: Int) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return
val path = Path()
path.moveTo(x.toFloat(), y.toFloat())
val builder = GestureDescription.Builder()
val gestureDescription = builder
.addStroke(GestureDescription.StrokeDescription(path, 50, 200))
.build()
val result = dispatchGesture(gestureDescription, @RequiresApi(Build.VERSION_CODES.N)
object : GestureResultCallback() {
override fun onCancelled(gestureDescription: GestureDescription?) {
super.onCancelled(gestureDescription)
Timber.i("onCancelled : gestureDescription")
}
override fun onCompleted(gestureDescription: GestureDescription?) {
super.onCompleted(gestureDescription)
Timber.i("onCompleted : gestureDescription")
}
}, null)
Timber.i("dispatch gesture: $result, x = $x, y = $y")
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/185023.html
標籤:Android
上一篇:qt4可以呼叫qt5的庫嗎?
