获取android屏幕的文本内容 [英] Get text content of the android screen

查看:54
本文介绍了获取android屏幕的文本内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以获取正在android手机屏幕上显示的文本内容.可能是任何Android应用程序提供的.

Is there any way to get the text content that is being displayed on the android mobile screen. It may be by any android app.

例如,在上面链接的图像中,我希望获取图像中的所有文本(从"Larry Page"开始到"Fred Smith"末尾以及屏幕上所有单词的位置)及其在屏幕上的位置.

for example in the image linked above, i wish to get all the text (starting from the "Larry Page" till the end "Fred Smith" and also the position of all the words on the screen) that is in the image and its respective position on the screen.

推荐答案

这可以使用Android的辅助功能来实现(与通用复制应用程序一样).

This can be achieved using the Accessibility feature of Android (as the Universal copy app does).

您需要做的是实现一个名为 AccessibilityService 的类.可以通过侦听器访问所有屏幕UI.

What you need to do is implement a class called AccessabilityService which can access all the screen UI through a listener.

为此,您需要在清单中声明它:

In order to that you need to declare it in the manifest:

 <service android:name=".MyAccessibilityService">
 <intent-filter>
     <action android:name="android.accessibilityservice.AccessibilityService" />
 </intent-filter>
 <meta-data android:name="android.accessibilityservice" android:resource="@xml/accessibilityservice" />

在文件@ xml/accessibilityservice中,您可以在其中配置服务,例如侦听哪个程序包或要获取哪种事件:

the file @xml/accessibilityservice is where you configure the service like to which package to listen or what kind of events you want to get:

<accessibility-service
 android:accessibilityEventTypes="typeViewClicked|typeViewFocused"
 android:packageNames="com.example.android.myFirstApp, com.example.android.mySecondApp"
 android:accessibilityFeedbackType="feedbackSpoken"
 android:notificationTimeout="100"
 android:settingsActivity="com.example.android.apis.accessibility.TestBackActivity"
 android:canRetrieveWindowContent="true"/>

在类中重写方法:

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    Log.v(TAG, String.format(
            "onAccessibilityEvent: type = [ %s ], class = [ %s ], package = [ %s ], time = [ %s ], text = [ %s ]",
            getEventType(event), event.getClassName(), event.getPackageName(),
            event.getEventTime(), getEventText(event)));
}

在该方法中,您应该在UI中找到所需的text/TextView/EditText并从中提取所需的文本.

In that method, you should find in the UI the desired text/TextView/EditText and extract from it the text you want.

注意:这种方式需要只有用户才能提供给您的可访问性权限,而不能在运行时完成.

您可以在Android文档中了解有关如何实现AccessibilityService的更多信息

You can read more in the Android documentation about how to implement an AccessabilityService

在评论中的问题之后,我添加了从屏幕上所有文本组件读取的代码:

after the question in the comment I'm adding the code to read from all the text components in the screen:

    private void printAllViews(AccessibilityNodeInfo mNodeInfo) {
        if (mNodeInfo == null) return;
        String log ="";
        for (int i = 0; i < mDebugDepth; i++) {
            log += ".";
        }
        log+="("+mNodeInfo.getText() +" <-- "+ 
        mNodeInfo.getViewIdResourceName()+")";
        Log.d(TAG, log);
        if (mNodeInfo.getChildCount() < 1) return;
        mDebugDepth++;

        for (int i = 0; i < mNodeInfo.getChildCount(); i++) {
            printAllViews(mNodeInfo.getChild(i));
        }
        mDebugDepth--;
    }

别忘了像这样初始化AccessibilityNodeInfo和深度计数器:

Just don't forget to initialize the AccessibilityNodeInfo and the depth counter like so:

public void onAccessibilityEvent(AccessibilityEvent event) {
    mDebugDepth = 0; 
    mNodeInfo = event.getSource();
    printAllViews(mNodeInfo);
 ...
}

这篇关于获取android屏幕的文本内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆