java - 有没有办法使用 AccessibilityService 单击对话框中的超链接?

标签 java android accessibilityservice android-11

permission prompt image

我正在尝试使用应用程序的辅助功能服务来单击“设置中允许”。我查看了 AccessibilityNode,但在 TextView 中没有看到任何可交互的内容。这是节点的输出:

Event Type: TYPE_WINDOW_CONTENT_CHANGED com.google.android.permissioncontroller android.widget.FrameLayout
 Source: 
0 | class name: android.widget.FrameLayout text: null content description: null input type 0 actions: ACTION_SELECT, ACTION_CLEAR_SELECTION, ACTION_ACCESSIBILITY_FOCUS, ACTION_SHOW_ON_SCREEN  
1 | class name: android.widget.ScrollView text: null content description: null input type 0 actions: ACTION_FOCUS, ACTION_SELECT, ACTION_CLEAR_SELECTION, ACTION_ACCESSIBILITY_FOCUS, ACTION_SHOW_ON_SCREEN  
2 | class name: android.widget.TextView text: Change location access for AppName? content description: null input type 0 actions: ACTION_SELECT, ACTION_CLEAR_SELECTION, ACTION_ACCESSIBILITY_FOCUS, ACTION_NEXT_AT_MOVEMENT_GRANULARITY, ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY, ACTION_SET_SELECTION, ACTION_SHOW_ON_SCREEN  
2 | class name: android.widget.TextView text: This app wants to access your location all the time, even when you’re not using the app. Allow in settings. content description: null input type 0 actions: ACTION_FOCUS, ACTION_SELECT, ACTION_CLEAR_SELECTION, ACTION_ACCESSIBILITY_FOCUS, ACTION_NEXT_AT_MOVEMENT_GRANULARITY, ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY, ACTION_SET_SELECTION, ACTION_SHOW_ON_SCREEN  
2 | class name: android.widget.Button text: Keep “While the app is in use” content description: null input type 0 actions: ACTION_FOCUS, ACTION_SELECT, ACTION_CLEAR_SELECTION, ACTION_CLICK, ACTION_ACCESSIBILITY_FOCUS, ACTION_NEXT_AT_MOVEMENT_GRANULARITY, ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY, ACTION_SET_SELECTION, ACTION_SHOW_ON_SCREEN 

TextView 只有少数可用的操作。据我所知,我已尝试了可用的操作,但没有成功。

我还探索了“在设置中允许”部分的直接 Intent 的想法,但目前还没有。我们的应用程序要求位置信息始终处于开启状态。

最佳答案

所以我最终做的是编写一个静态函数来从 CharSequence 获取跨度

@NonNull
ClickableSpan[] getClickableSpans(CharSequence text) {
    try {
      if (text instanceof Spanned) {
        Spanned spanned = (Spanned) text;
        return spanned.getSpans(0, text.length(), ClickableSpan.class);
      }
    } catch (Exception e) {
    //log exception
    }
    return new ClickableSpan[0];
}

并使用它

public void onAccessibilityEvent(AccessibilityEvent) {
    AccessibilityNodeInfo nodeInfo = event.getSource();
    if (nodeInfo != null) {
        List<AccessibilityNodeInfo> nodeInfoList = nodeInfo.findAccessibilityNodeInfosByText(
          "This app wants to access your location all the time, even when you're not using the app. Allow in settings.");
        if (nodeInfoList != null && !nodeInfoList.isEmpty()) {
            ClickableSpan[] spans = getClickableSpans(nodeInfoList.get(0).getText());
            if (spans.length > 0) {
                spans[0].onClick(null);
            }
        }
    }
}

关于java - 有没有办法使用 AccessibilityService 单击对话框中的超链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62219663/

相关文章:

java - ANDROID:错误 W/DynamiteModule:找不到 com.google.android.gms.googlecertificates 的本地模块描述符类

java - Spring Boot 过滤 : At least one property must be given

android - 在Android中使用辅助服务获取 Activity 的布局名称

android - 使用无障碍服务

java - Android 旋转器未选择任何项目

java - 为什么 Exception.getMessage() 在 postgres jdbc 8.3 和 jdbc 8.4 版本之间不同?

android - 替换 MapView OnLongPress 监听器

java - Android 工作管理器与服务?

android - 像 View 寻呼机一样实现 View

android - android accessibilityService 会在低于 30 的 api 上截屏吗?