Adb 方式
With the help of adb shell commands, we can simulate an action to perform click coordinates using the following method.
1、adb shell input tap x y
However, there are some thresholds for ADB operations
Requires a computer to execute ADB commands (terminal execution)
A data cable is required
The target device (mobile) needs developer mode on
The problem with all ADB operations is that they cannot be done by a single device. Therefore, auxiliary services can be used to achieve the independent completion of a single device.
辅助功能
Accessibility in Android is a very dark technology. With the following code, we can implement the coordinate based click.
@RequiresApi(Build.VERSION_CODES.N)
fun AccessibilityService.dispatchClick(rect: Rect?) {
rect ?: return
val x = rect.middleVertically()
val y = rect.middleHorizontally()
dispatchClick(x, y)
}
@RequiresApi(Build.VERSION_CODES.N)
fun AccessibilityService.dispatchClick(x: Float, y: Float) {
val path = Path()
path.moveTo(x, y)
smartLogD {
"dispatchClick x=$x y=$y"
}
path.lineTo(x + 1, y)
val builder = GestureDescription.Builder()
builder.addStroke(GestureDescription.StrokeDescription(path, 0,
ViewConfiguration.getTapTimeout().toLong()
))
this.dispatchGesture(builder.build(), null, null)
}
开始使用
1.在项目根目录下的 build.gradle 增加仓库配置
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
2.在模块下的 build.gradle 增加依赖引用
dependencies {
implementation 'com.github.androidyue:coobox:0.8.5'
}
x.y.z 为最新的版本信息