问题:在使用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