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

Android中用Java实现全局屏幕指定位置自动点击及权限说明

Android全局屏幕自动点击实现方案(Java)

原有代码问题分析

你的现有代码存在两个核心问题:

  1. 仅向当前应用的DecorView分发触摸事件,只能作用于自身应用内,无法实现全局点击。
  2. 只发送了ACTION_DOWN事件,缺少对应的ACTION_UP事件——完整的点击需要按下+抬起两个动作,这也是代码无法正常工作的直接原因。

全局点击实现方案

方案一:AccessibilityService(无Root推荐)

这是Android官方提供的无障碍服务,可实现全局UI交互操作,无需Root,但需要用户手动开启无障碍权限。

实现步骤:

  1. 创建无障碍服务子类
public class GlobalClickService extends AccessibilityService {

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        // 可在此处处理系统界面变化等事件
    }

    @Override
    public void onInterrupt() {
        // 服务中断时的逻辑处理
    }

    // 全局点击方法
    public void performGlobalClick(int x, int y) {
        GestureDescription.Builder builder = new GestureDescription.Builder();
        Path path = new Path();
        path.moveTo(x, y);
        // 添加按下+抬起的手势动作,100ms间隔模拟真实点击
        builder.addStroke(new GestureDescription.StrokeDescription(path, 0, 100));
        // 执行全局手势
        dispatchGesture(builder.build(), new GestureResultCallback() {
            @Override
            public void onCompleted(GestureDescription gestureDescription) {
                super.onCompleted(gestureDescription);
                // 点击完成后的回调逻辑
            }

            @Override
            public void onCancelled(GestureDescription gestureDescription) {
                super.onCancelled(gestureDescription);
                // 点击取消后的回调逻辑
            }
        }, null);
    }
}
  1. 配置无障碍服务参数(在res/xml目录下创建accessibility_service_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"
    android:canPerformGestures="true"
    android:description="@string/accessibility_service_description" />
  1. 在AndroidManifest.xml中注册服务
<service
    android:name=".GlobalClickService"
    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_service_config" />
</service>
  1. 引导用户开启权限
    在Activity中调用以下代码,跳转到系统无障碍设置页面让用户手动开启服务:
Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivity(intent);

方案二:Instrumentation(需Root/系统权限)

如果设备有Root权限,或者你的应用是系统签名应用,可以使用Instrumentation直接发送全局触摸事件,方式更直接。

实现代码:

注意:必须在子线程中执行,避免阻塞主线程

private void performGlobalClickWithRoot(int x, int y) {
    new Thread(() -> {
        Instrumentation instrumentation = new Instrumentation();
        // 发送按下事件
        instrumentation.sendPointerSync(MotionEvent.obtain(
            SystemClock.uptimeMillis(),
            SystemClock.uptimeMillis(),
            MotionEvent.ACTION_DOWN,
            x, y, 0
        ));
        // 模拟真实点击的间隔
        SystemClock.sleep(100);
        // 发送抬起事件
        instrumentation.sendPointerSync(MotionEvent.obtain(
            SystemClock.uptimeMillis(),
            SystemClock.uptimeMillis(),
            MotionEvent.ACTION_UP,
            x, y, 0
        ));
    }).start();
}

所需权限说明

  • AccessibilityService方案:
    无需在Manifest中额外声明权限,但需要用户手动在系统设置中开启对应应用的无障碍权限(android.permission.BIND_ACCESSIBILITY_SERVICE是绑定服务的必要权限,已在注册服务时声明)。
  • Instrumentation方案:
    • Root环境:应用需获取Root权限,在Root进程中执行代码;
    • 系统应用:应用需使用系统签名,并在Manifest中声明android.permission.INJECT_EVENTS权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 05:32:37