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

目录
相关文章
|
Java Android开发
【Android 开发入门】为按钮添加Click单击事件处理程序,显示/隐藏另一个按钮
在上篇“走进Android开发的世界,HelloWorld”,我们创建了一个Android 项目 HelloWorld,并演示了如何通过USB连接手机查看运行效果;这里讲一下如何为应用添加一个按钮,并为按钮添加Click单击事件处理程序,显示/隐藏另一个按钮。
1781 0
|
Android开发
大叔也说Xamarin~Android篇~ListView里的Click事件并获取本行的其它元素
原文:大叔也说Xamarin~Android篇~ListView里的Click事件并获取本行的其它元素 我原创,我贡献,我是仓储大叔 本篇大叔原创,本着对技术的热爱去研究它,把成果分享给国人!大叔始终相信一句话:你只有选择一个感兴趣的工作,你才能更好的发挥你的潜力,而这一切都建立在你不断研究,不断钻研的前提下。
1083 0
|
Android开发
Xamarin for android:为button设置click事件的几种方法
原文:Xamarin for android:为button设置click事件的几种方法 在Xamarin中一个最基础的事情,就是为一个button指定click事件处理方法,可是即使是这么一件事也有好几种方法,我在下面列出几种。
1765 0
|
11天前
|
存储 安全 Android开发
安卓应用开发:构建一个高效的用户登录系统
【5月更文挑战第3天】在移动应用开发中,用户登录系统的设计与实现是至关重要的一环。对于安卓平台而言,一个高效、安全且用户体验友好的登录系统能够显著提升应用的用户留存率和市场竞争力。本文将探讨在安卓平台上实现用户登录系统的最佳实践,包括对最新身份验证技术的应用、安全性考量以及性能优化策略。
|
13天前
|
前端开发 Android开发 iOS开发
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
【4月更文挑战第30天】Flutter 框架实现跨平台移动应用,通过一致的 UI 渲染(Skia 引擎)、热重载功能和响应式框架提高开发效率和用户体验。然而,Android 和 iOS 的系统差异、渲染机制及编译过程影响性能。性能对比显示,iOS 可能因硬件优化提供更流畅体验,而 Android 更具灵活性和广泛硬件支持。开发者可采用代码、资源优化和特定平台优化策略,利用性能分析工具提升应用性能。
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
|
4天前
|
Java Android开发
Android开发--Intent-filter属性详解
Android开发--Intent-filter属性详解