You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android自动点击编程:如何通过编程方式按X、Y坐标执行屏幕点击

Android 编程实现指定坐标点击操作的正确方案

1. 前置权限说明

  • 系统签名级应用可直接申请android.permission.INJECT_EVENTS权限注入触摸事件
  • 普通第三方应用无系统签名时,优先选择无障碍服务方案实现模拟点击
  • Root设备可直接通过执行Shell命令完成点击,无需额外应用层权限

2. 不同场景的具体实现

2.1 无障碍服务实现(适配无Root普通设备,兼容性最佳)

首先创建自定义无障碍服务类,核心点击逻辑如下:

// x、y为要点击的坐标
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    GestureDescription.Builder gestureBuilder = new GestureDescription.Builder();
    Path clickPath = new Path();
    clickPath.moveTo(x, y);
    gestureBuilder.addStroke(new GestureDescription.StrokeDescription(clickPath, 0, 100));
    dispatchGesture(gestureBuilder.build(), null, null);
}

在AndroidManifest.xml中注册服务:

<service
    android:name=".YourCustomAccessibilityService"
    android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
    <intent-filter>
        <action android:name="android.accessibilityservice.AccessibilityService" />
    </intent-filter>
    <meta-data
        android:name="android.accessibilityservice"
        android:resource="@xml/accessibility_config" />
</service>

在res/xml目录下新建accessibility_config.xml配置文件:

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagDefault|flagRequestTouchExplorationMode"
    android:canPerformGestures="true"
    android:description="@string/accessibility_service_tip" />

使用前需要引导用户手动在系统设置的无障碍列表中开启对应服务权限,点击操作才会生效

2.2 Root设备Shell命令实现(无需应用权限)

直接执行Shell点击命令即可:

input tap 横坐标 纵坐标

代码中调用示例:

try {
    Process suProcess = Runtime.getRuntime().exec("su");
    DataOutputStream outputStream = new DataOutputStream(suProcess.getOutputStream());
    outputStream.writeBytes("input tap " + x + " " + y + "\n");
    outputStream.writeBytes("exit\n");
    outputStream.flush();
    suProcess.waitFor();
} catch (Exception e) {
    e.printStackTrace();
}

2.3 系统签名应用实现

已获取系统签名的应用,申请对应权限后可通过InputManager注入触摸事件:

InputManager inputManager = (InputManager) getSystemService(Context.INPUT_SERVICE);
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
// 注入按下事件
MotionEvent downEvent = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0);
inputManager.injectInputEvent(downEvent, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
// 注入抬起事件
MotionEvent upEvent = MotionEvent.obtain(downTime, eventTime + 100, MotionEvent.ACTION_UP, x, y, 0);
inputManager.injectInputEvent(upEvent, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
downEvent.recycle();
upEvent.recycle();

同时在AndroidManifest.xml添加权限声明:

<uses-permission android:name="android.permission.INJECT_EVENTS" />

内容的提问来源于stack exchange,提问作者eyal hadad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 01:09:05