通过我的应用读取另一个应用的内容

Read content of another app through my app

几天来我一直在寻找同样的问题。但无法得到任何提示。

我需要创建一个像 voodoo 应用这样的应用,它只在 flipkart 等不同应用的特定页面上显示其自定义布局

现在,到目前为止,我已经找到了使用 AccessebilityService 和 MediaProjection 类 的选项。但是我被卡住了,我怎么能以编程方式知道 Flipkart 的产品详细信息页面是可见的,以便我可以像 Voodoo 应用程序一样在其上显示我的应用程序的自定义视图。

有什么建议吗?

您要做的是以下内容。

使用无障碍服务跟踪传入事件。然后您想跟踪 TYPE_WINDOW_CONTENT_CHANGED 事件,并检测 window 内容何时符合您的预期。

@Override
public void onAccessibilityEvent(AccessibilityEvent e) {

    switch (e.getEventType()) {
        case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:  {
            if (isFlipkartProdcutDetailPage(getRootInActiveWindow()) {
                doStuff()
            }
        }
    }
}
public boolean isFlipkartProductDetailPage(AccessibilityNodeInfo nodeInfo) {
    //Use the node info tree to identify the proper content.
    //For now we'll just log it to logcat.
    Log.w("TAG", toStringHierarchy(nodeInfo, 0));
}

private String toStringHierarchy(AccessibilityNodeInfo info, int depth) {
    if (info == null) return "";

    String result = "|";
    for (int i = 0; i < depth; i++) {
        result += "  ";
    }

    result += info.toString();

    for (int i = 0; i < info.getChildCount(); i++) {
        result += "\n" + toStringHierarchy(info.getChild(i), depth + 1);
    }

    return result;
}