代码之家  ›  专栏  ›  技术社区  ›  Jayce

Android可点击屏幕文本

  •  8
  • Jayce  · 技术社区  · 9 年前

    protected void processText(String text)
    {
      if (text.contains("Ready"))
          // click the ready text
    }
    
    2 回复  |  直到 9 年前
        1
  •  4
  •   Abraham Toledo    9 年前

    我使用 AccessibilityService 。它只能在API级别上正常工作>=16尽管如此。

    可访问性服务 例如,该类将获取USSD响应的文本并关闭对话框。

    // ....
    public class UssdAccessibilityService extends AccessibilityService {
        public UssdAccessibilityService() {
        }
    
        @TargetApi(16)
        @Override
        public void onAccessibilityEvent(AccessibilityEvent event) {
            if (!"com.android.phone".equalsIgnoreCase((String)event.getPackageName())){
                // In this example we are only interested in events comming
                // from "com.android.phone" package
                event.recycle();
                return;
            }
    
            String className = (String)event.getClassName();
            if (className == null || (!className.contains("AlertDialog") && !className.contains("AlertDialog"))){
                // Class is not an USSD dialog
                event.recycle();
                return;
            }
    
            AccessibilityNodeInfo source = event.getSource();
            if (source == null) {
                // getSource() is annotated @Nullable, so we do this to be
                // safe just in case
                event.recycle();
                return;
            }
    
            AccessibilityNodeInfo acceptButton = null;
            String ussdText = null;
    
            int childCount = source.getChildCount();
            for (int i = 0; i < childCount; i++){
                AccessibilityNodeInfo current = source.getChild(i);
                if (current == null)
                    continue;
    
                String currentText = (String)current.getText();
                if (current.isClickable()){
                    // In the case of USSD dialogs, there is only one clickable.
                    // May be necessary to do more robust search in other scenarios
                    acceptButton = current;
                    continue;
                }
    
                ussdText = currentText;
    
                current.recycle();
            }
    
            if (ussdText!= null) {
                if (acceptButton != null)
                    acceptButton.performAction(AccessibilityNodeInfo.ACTION_CLICK);
    
            }
    
            source.recycle();
            event.recycle();
        }
    
        // ....
    }
    

    您必须在下的清单中声明可访问性服务 <application>

    <service
            android:name=".UssdAccessibilityService"
            android:enabled="true"
            android:label="Read USSD codes and dismiss"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>
    
            <meta-data
                android:name="android.accessibilityservice"
                android:resource="@xml/accessibility_service_config" />
    </service>
    

    res/xml 创造 accessibility_service_config.xml

    <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
        android:description="@string/accessibility_service_description"
        android:packageNames="com.android.phone,com.ats.android.activationcodebot"
        android:accessibilityEventTypes="typeWindowStateChanged|typeWindowContentChanged"
        android:accessibilityFlags="flagDefault"
        android:accessibilityFeedbackType="feedbackSpoken"
        android:notificationTimeout="100"
        android:canRetrieveWindowContent="true"
        />
    

    当然,您必须根据自己的需要调整此代码。

    设置(>);可访问性 在Android中(或要求用户这样做)。

    阅读更多。。。 Developing an Accessibility Service

        2
  •  3
  •   ziLk    9 年前

    当另一个应用程序处于活动状态时,您的应用程序应该处于活动状态才能捕获屏幕。所以你只能使用android服务,它总是在后台工作。

    activity ,您的 service 所以你必须做一个 TimerTask 这将调用您每小时的活动和您的 用当前显示视图对其进行响应,您可以从中捕获屏幕截图。

    ,并读取 framebuffer 对于那个将给出当前屏幕的原始数据,然后将其转换为位图或任何图片文件,你们可以在你们的服务中这样做。

    简而言之,这可能适用于根手机。否则,如果你是非根用户,你就不能截取其他应用的屏幕截图。只允许拍摄应用程序的屏幕截图。

    能够分析屏幕是否有任何“就绪”文本。您的应用程序应该处于活动状态,以便能够捕获屏幕。