如何执行点击开关按钮(打开/关闭)的操作?

时间:2019-02-27 18:03:35

标签: android accessibilityservice

我正在尝试在智能手机Android 7.1上测试以下代码,以尝试单击切换按钮(打开 / 关闭):

enter image description here

但是什么也没发生,尽管这表明是成功的。

如何解决这个问题?

private void iteratorTXDM(AccessibilityNodeInfo info) {
    boolean found = false;
    List<AccessibilityNodeInfo> node = info.findAccessibilityNodeInfosByViewId("android:id/switch_widget");
    if (node != null) {
        for (AccessibilityNodeInfo element : node) {
            element.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            System.out.println("The element was clicked!!");
        }
        //iteratorTXDM(getRootInActiveWindow());
        found = true;
        return;
    } else {
        for (int i = 0; i < info.getChildCount(); i++) {
            if (info.getChild(i) != null) {
                if (found) {
                    found = false;
                    break;
                }
                iteratorTXDM(info.getChild(i));
            }
        }
    }
    return;
}

private void click(){

    iteratorTXDM(source);
}

AccessibilityNodeInfo source;

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {

    source = event.getSource();

    if (source == null) {
        return;
    }
}

accessibility_service_config.xml

<?xml version="1.0" encoding="utf-8"?>

<accessibility-service
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFeedbackType="feedbackAllMask"
    android:accessibilityFlags="flagDefault|flagIncludeNotImportantViews|flagReportViewIds"
    android:canPerformGestures="true"
    android:canRetrieveWindowContent="true"
    android:description="@string/accessibility_service_description"
    android:notificationTimeout="100"
    tools:ignore="UnusedAttribute" />

0 个答案:

没有答案