0

权限提示图片

我正在尝试使用我的应用程序的辅助功能服务来使用辅助功能单击“允许设置”。我查看了 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 只有少数几个可用的操作。据我所知,我已经尝试了可用的操作,但没有成功。

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

4

2 回答 2

1

所以我最终做的是编写一个静态函数来从 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);
            }
        }
    }
}
于 2020-06-11T20:08:12.977 回答
0

从 Android O 开始,ClickableSpans 被传递给辅助功能。您可以在文本中找到 ClickableSpans 并调用 onClick。然后系统会确保在应用进程中调用了原始 span 的 onClick 方法。

如果您使用 AccessibilityNodeInfoCompat,这是向后移植的,但应用程序还必须调用 ViewCompat#enableAccessibleClickableSpanSupport(view)。

于 2020-06-05T18:34:56.917 回答