我试图访问另一个可能使用非原生(js + html)框架构建的应用程序的文本内容。
因此,我想我会尝试从对应于WebView元素的辅助功能节点访问数据。 但是,我无法使用常用方法获取textual / html数据,因为像getText()这样的方法只有在它是TextView,Button等本机android元素时才能工作。
public class MyAccessibilityService extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
AccessibilityNodeInfo accessibilityNodeInfo = accessibilityEvent.getSource();
if (accessibilityNodeInfo == null) {
return;
}
int childCount = accessibilityNodeInfo.getChildCount();
for (int i = 0; i < childCount; i++) {
AccessibilityNodeInfo accessibilityNodeInfoChild = accessibilityNodeInfo.getChild(i);
myRecursiveFunc(accessibilityNodeInfoChild);
}
}
@Override
public void onInterrupt() {
}
private void myRecursiveFunc(AccessibilityNodeInfo accessibilityNodeInfoChild) {
if (accessibilityNodeInfoChild.getChildCount() > 0) {
for (int j = 0; j < accessibilityNodeInfoChild.getChildCount(); j++) {
AccessibilityNodeInfo child = accessibilityNodeInfoChild.getChild(j);
if (child != null) {
myRecursiveFunc(child);
}
}
} else {
if ("android.webkit.WebView".equals(accessibilityNodeInfoChild.getClassName())) {
//===========This is a WebView's AccessibilityNodeInfo !!!!
//===========How to get HTML data from nodeinfo object here ??
}
}
}
}
答案 0 :(得分:9)
您的服务是否有FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY?
final AccessibilityServiceInfo info = getServiceInfo();
info.flags |= AccessibilityServiceInfo.FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY;
setServiceInfo(info);
Android默认的AccessibilityService(TalkBack)要求能够屏幕阅读WebView
中的文字。从检查源代码来看,似乎有一些脚本注入和一些轻微的hackiness,以便能够获取HTML元素的文本内容
你应该查看他们的来源: