我正在尝试在智能手机Android 7.1上测试以下代码,以尝试单击切换按钮(打开 / 关闭):
但是什么也没发生,尽管这表明是成功的。
如何解决这个问题?
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" />