Get the text contents of an Android screen

Is there a way to get the text content that is displayed on the screen of the Android mobile. It can be any Android app.

http://i.stack.imgur.com/vMPcD.jpg

for example, in the image above, I want to get all the text (from “Larry Page” to the end of “Fred Smith”, as well as the position of all the words on the screen), which is on the image and its corresponding position on the screen.

+9
source share
2 answers

This can be achieved using the Android accessibility feature (as Universal copy does).

, AccessabilityService, .

:

 <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 - , , , :

<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)));
}

/TextView/EditText .

: , , .

Android , AccessabilityService

: :

    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 :

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

, "uiautomatorviewer". bin Android SDK/tools/bin/folder.

uiautomatorviewer .

$./uiautomatorviewer
  • , USB .
  • uiautomator
  • , "Node " .

enter image description here

- .

, android.getText() method

+1

All Articles