我有一个方法可以通过 Assist API 遍历所有 ViewNode:
@Override
public void onHandleAssist(Bundle data, AssistStructure structure, AssistContent content) {
super.onHandleAssist(data, structure, content);
mContent = "";
mDescription = "";
String structuredData = content.getStructuredData();
if (!TextUtils.isEmpty(structuredData)) {
structuredData = structuredData.replace("\\", "");
mContent += structuredData + " ";
}
Bundle extra = content.getExtras();
ClipData clipData = content.getClipData();
Intent intent = content.getIntent();
Bundle intentExtra = intent.getExtras();
Uri uri = content.getWebUri();
if (uri != null) {
mContent += uri.toString() + " ";
}
ComponentName componentName = structure.getActivityComponent();
int nodeCount = structure.getWindowNodeCount();
for (int i = 0; i < nodeCount; i++) {
AssistStructure.WindowNode windowNode = structure.getWindowNodeAt(i);
getAllContentText(windowNode.getRootViewNode(), 5);
}
initView();
}
private void getAllContentText(AssistStructure.ViewNode node, int levelMax) {
if (node != null) {
int nodeCount = node.getChildCount();
if (!TextUtils.isEmpty(node.getText())) {
mContent += node.getText().toString() + " ";
}
if (!TextUtils.isEmpty(node.getContentDescription())) {
mDescription += node.getContentDescription() + " ";
}
for (int i = 0; i < nodeCount; i++) {
AssistStructure.ViewNode childNode = node.getChildAt(i);
getAllContentText(childNode, levelMax);
}
}
}
但是如果一个文本被省略了,我怎样才能得到所有的文本呢?或者如果文本是链接文本,我可以获取与之关联的 url 吗?
最佳答案
我相信您不能直接引用应用程序的 View ,您从 AssistStructure 获得的只是 AssistStructure.ViewNode。 View 节点的 getText() 方法将可见文本返回给用户。
也许如果您找到某种方法通过您的 VoiceInteractionSession getContext() 方法引用 Window 对象。您可以将上下文转换为 Activity 或 Window,然后调用 getDecorView(),然后遍历 subview 以从 TextView 获取完整文本。
关于android - 从 Android Assist API 检索所有文本内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37295155/