accessservice对于难定位的view如何定位
private static int tabcount = -1;
private static StringBuilder sb;
public static void printPacketInfo(AccessibilityNodeInfo root) {
sb = new StringBuilder();
tabcount = 0;
int[] is = {};
analysisPacketInfo(root, is);
JLog.d(sb.toString());
}
//打印此时的界面状况,便于分析
private static void analysisPacketInfo(AccessibilityNodeInfo info, int... ints) {
if (info == null) {
return;
}
if (tabcount > 0) {
for (int i = 0; i < tabcount; i++) {
sb.append("\t\t");
}
}
if (ints != null && ints.length > 0) {
StringBuilder s = new StringBuilder();
for (int j = 0; j < ints.length; j++) {
s.append(ints[j]).append(".");
}
sb.append(s).append(" ");
}
String name = info.getClassName().toString();
String[] split = name.split("\\.");
name = split[split.length - 1];
if ("TextView".equals(name)) {
CharSequence text = info.getText();
sb.append("text:").append(text);
} else if ("Button".equals(name)) {
CharSequence text = info.getText();
sb.append("Button:").append(text);
} else {
sb.append(name);
}
sb.append("\n");
int count = info.getChildCount();
if (count > 0) {
tabcount++;
int len = ints.length + 1;
int[] newInts = Arrays.copyOf(ints, len);
for (int i = 0; i < count; i++) {
newInts[len - 1] = i;
analysisPacketInfo(info.getChild(i), newInts);
}
tabcount--;
}
}
该方法打印的节点树如下:

这样我们可以通过前面的0.0.0.1.1直接定位到View
AccessibilityNodeInfo info = root;
int[] path = {0, 0, 0, 1, 1};
for (int i = 0; i < path.length; i++) {
info = info.getChild(path[i]);
if (info == null || info.getChildCount() <= 0) {
return null;
}
}
return info;
当然你有可能不知道0.0.0.1.1对应哪一个视图,可以通过
Rect rect = new Rect();
info.getBoundsInScreen(rect);
//状态栏的高度
int h = GUtil.getStatusBarHeight(context.getApplicationContext());
rect.top -= h;
rect.bottom -= h;

浙公网安备 33010602011771号