【AccessibilityService】按钮无法点击问题

问题:在使用Android AccessibilityService的时候,会发现一些按钮无法点击(clickable是false),如果还是使用“node.performAction(AccessibilityNodeInfo.ACTION_CLICK)”,则不会发出点击事件。

解决方案:
1. APK获取系统root权限,模拟发送坐标“input tap x y”。
2. 使用dispatchGuesture()发送点击事件,不需要root。
这里强烈推荐方案2,对方案1感兴趣的可以baidu相关代码(可以参考https://blog.csdn.net/weixin_33966095/article/details/91371705)。

代码修改步骤:
1. 在“AccessibilityService”配置xml文件中加入申请手势的权限“android:canPerformGestures="true"”,

<?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:canRetrieveWindowContent="true"
    android:canPerformGestures="true" //申请手势权限
    android:description="@string/accessibility_desc" //简介
    android:notificationTimeout="1000" //通知时间
    android:packageNames="com.oo.android.xxx.xxx" /> //包名

2. 模拟点击事件

    private void tap(int x, int y) {
        Log.i(TAG,"tap++");

        GestureDescription.Builder builder = new GestureDescription.Builder();
        Path p = new Path();
        p.moveTo(x , y);
        builder.addStroke(new GestureDescription.StrokeDescription(p, 0L, 500L));
        GestureDescription gesture = builder.build();
        dispatchGesture(gesture, new GestureResultCallback() {
            @Override
            public void onCompleted(GestureDescription gestureDescription) {
                super.onCompleted(gestureDescription);
                Log.i(TAG, "onCompleted...");
            }

            @Override
            public void onCancelled(GestureDescription gestureDescription) {
                super.onCancelled(gestureDescription);
                Log.e(TAG, "onCancelled...");
            }
        }, null);
    }

3. 模拟滑动事件

    private void swipe(int xStart, int yStart, int xEnd , int yEnd , long startTime , long duration) {
        Log.e(TAG,"swipe++");

        GestureDescription.Builder builder = new GestureDescription.Builder();
        Path p = new Path();
        p.moveTo(xStart , yStart);
        p.lineTo(xEnd , yEnd);
        builder.addStroke(new GestureDescription.StrokeDescription(p, startTime, duration));
        GestureDescription gesture = builder.build();
        dispatchGesture(gesture, new GestureResultCallback() {
            @Override
            public void onCompleted(GestureDescription gestureDescription) {
                super.onCompleted(gestureDescription);
                Log.e(TAG, "onCompleted...");
            }

            @Override
            public void onCancelled(GestureDescription gestureDescription) {
                super.onCancelled(gestureDescription);
                Log.e(TAG, "onCancelled...");
            }
        }, null);
    }

4. 参考资料
https://blog.csdn.net/czero000/article/details/102970233
https://blog.csdn.net/littlefishvc/article/details/80057841
https://blog.csdn.net/weixin_33966095/article/details/91371705

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值