我正在尝试使用 AccessibilityService
. 到目前为止还不错,但我的代码未能从使用 WebView
. 在我的例子中,内容无法检索的应用程序之一是三星浏览器。然而,当我在手机中启动对讲功能时,我的代码突然能够检索到通话内容 WebView
.
以下是文件中我的辅助功能服务的配置 accessibility_config.xml
:
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFlags="flagIncludeNotImportantViews|flagRequestEnhancedWebAccessibility"
android:accessibilityFeedbackType="feedbackGeneric
android:notificationTimeout="100"
android:canRetrieveWindowContent="true" />
这是我的服务代码:
class MyAccessibility : AccessibilityService() {
var activeWindowId: Int? = null
override fun onInterrupt() {}
override fun onAccessibilityEvent(e: AccessibilityEvent?) {
if (e == null) return
// Handle window changed
if (e.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
activeWindowId = rootInActiveWindow?.windowId
return
}
// Handle other events
// Make sure window didn't change
val windowId = rootInActiveWindow?.windowId
val windowChanged = (windowId == null || windowId != activeWindowId)
if (windowChanged) return
// Retrieve content
val typeString = AccessibilityEvent.eventTypeToString(e.eventType)
Log.d("MY-SERVICE", "$typeString: ${e.className} ${e.source?.contents()}")
}
private fun AccessibilityNodeInfo?.content(): String? {
if (this == null) return null
var text = text ?: contentDescription
if (text == null || text == "null") return null
text = text.replace(rxSpaces, " ").trim()
if (text.isEmpty()) return null
return text.toString()
}
private fun AccessibilityNodeInfo?.contents(): LinkedHashSet<String>? {
// Make sure node is not null
if (this == null) return null
// Fetch content of the node
val contents = LinkedHashSet<String>()
this.content()?.let { if (it.wordCount() > 5) contents.add(it) }
// Fetch content in children
for (i in 0 until childCount) {
val child = getChild(i)
child.contents()?.let { contents.addAll(it) }
child.recycle()
}
return contents
}
}
以下是我的服务在前台运行三星浏览器时的屏幕广播:
如您所见,我的服务无法检索任何内容 WebView
根本没发现。在我启动对讲机后(注意我用两个手指滚动):
如您所见,现在我的服务能够检索内容。
为什么会这样?我应该更改代码的哪一部分,使其始终能够检索内容,即使禁用了对讲功能?
谢谢您。
暂无答案!
目前还没有任何答案,快来回答吧!