In Android, AccessibilityService is used to simulate click events

简介: If you want to perform some simulated click action in Android, you usually have to use ADB and accessibility without modifying the page's source code.

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 为最新的版本信息

目录
相关文章
|
XML Android开发 数据格式
Android AccessibilityService无障碍服务(二)
当服务未开启时,快速的跳转到开启服务的界面。 if (!OpenAccessibilitySettingHelper.isAccessibilitySettingsOn(this, AccessibilitySampleService.
1663 0
|
XML Android开发 数据格式
Android AccessibilityService无障碍服务(一)
创建一个类AccessibilitySampleService继承自AccessibilityService,并实现其接口方法,onAccessibilityEvent与onInterrupt。
2647 0
|
Java Android开发
【Android 开发入门】为按钮添加Click单击事件处理程序,显示/隐藏另一个按钮
在上篇“走进Android开发的世界,HelloWorld”,我们创建了一个Android 项目 HelloWorld,并演示了如何通过USB连接手机查看运行效果;这里讲一下如何为应用添加一个按钮,并为按钮添加Click单击事件处理程序,显示/隐藏另一个按钮。
1836 0
|
Android开发
大叔也说Xamarin~Android篇~ListView里的Click事件并获取本行的其它元素
原文:大叔也说Xamarin~Android篇~ListView里的Click事件并获取本行的其它元素 我原创,我贡献,我是仓储大叔 本篇大叔原创,本着对技术的热爱去研究它,把成果分享给国人!大叔始终相信一句话:你只有选择一个感兴趣的工作,你才能更好的发挥你的潜力,而这一切都建立在你不断研究,不断钻研的前提下。
1107 0