最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How we can consistently detect YouTube Shorts screen in an Android Accessibility Service? - Stack Overflow

programmeradmin1浏览0评论

I'm developing an Android Accessibility Service that detects when a user is scrolling through YouTube Shorts and takes action accordingly. However, the detection is inconsistent because the view ID for Shorts sometimes works and sometimes does not.

Current Implementation:

Right now, I use the following function to check if the Shorts screen is active:

private fun isYouTubeShorts(node: AccessibilityNodeInfo): Boolean {
    node.findAccessibilityNodeInfosByViewId("com.google.android.youtube:id/reel_watch_fragment_root")
        ?.let {
            if (it.isNotEmpty()) {
                return true
            }
        }
    node.findAccessibilityNodeInfosByViewId("com.google.android.youtube:id/reel_recycler")
        ?.let {
            if (it.isNotEmpty()) {
                return true
            }
        }
    return false
}

Issue: Sometimes this works perfectly, but at other times, it fails to detect Shorts even though scrolling in YouTube is detected. When I close and reopen the app, detection starts working again. I think YouTube might be dynamically changing the view ID or rendering the UI asynchronously?

I tried a delay before checking rootInActiveWindow.

 override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        event?.let {
//            Log.d("TAG", "Event detected: ${event.eventType}, package: ${event.packageName}, source: ${event.source}")

//            if (event.eventType == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
            Log.d("TAG", "Scroll detected in package: ${event.packageName}")

            Handler(Looper.getMainLooper()).postDelayed({
                val rootNode = rootInActiveWindow
                if (rootNode != null) {
                    handleDetectedScrolling(event.packageName.toString(), rootNode)
                }
            }, 1000)

If anyone has worked on something similar, I'd really appreciate your help. Thanks in advance!!!!

I'm developing an Android Accessibility Service that detects when a user is scrolling through YouTube Shorts and takes action accordingly. However, the detection is inconsistent because the view ID for Shorts sometimes works and sometimes does not.

Current Implementation:

Right now, I use the following function to check if the Shorts screen is active:

private fun isYouTubeShorts(node: AccessibilityNodeInfo): Boolean {
    node.findAccessibilityNodeInfosByViewId("com.google.android.youtube:id/reel_watch_fragment_root")
        ?.let {
            if (it.isNotEmpty()) {
                return true
            }
        }
    node.findAccessibilityNodeInfosByViewId("com.google.android.youtube:id/reel_recycler")
        ?.let {
            if (it.isNotEmpty()) {
                return true
            }
        }
    return false
}

Issue: Sometimes this works perfectly, but at other times, it fails to detect Shorts even though scrolling in YouTube is detected. When I close and reopen the app, detection starts working again. I think YouTube might be dynamically changing the view ID or rendering the UI asynchronously?

I tried a delay before checking rootInActiveWindow.

 override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        event?.let {
//            Log.d("TAG", "Event detected: ${event.eventType}, package: ${event.packageName}, source: ${event.source}")

//            if (event.eventType == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
            Log.d("TAG", "Scroll detected in package: ${event.packageName}")

            Handler(Looper.getMainLooper()).postDelayed({
                val rootNode = rootInActiveWindow
                if (rootNode != null) {
                    handleDetectedScrolling(event.packageName.toString(), rootNode)
                }
            }, 1000)

If anyone has worked on something similar, I'd really appreciate your help. Thanks in advance!!!!

Share Improve this question asked Jan 30 at 15:09 Vishal AnandVishal Anand 337 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0
private fun handleShortsDetection(packageName: String, maxRetries: Int = 3) {
        var retryCount = 0
        val handler = Handler(Looper.getMainLooper())
        val checkRunnable = object : Runnable {
            override fun run() {
                val rootNode = rootInActiveWindow
                if (rootNode != null) {
                    if (isYouTubeShorts(rootNode)) {

                    } else if (retryCount < maxRetries) {
                        retryCount++
                        handler.postDelayed(this, 300) 
                    }
                }
            }
        }
        handler.post(checkRunnable)
}

Instead of a fixed delay, implement a retry loop with incremental checks to account for asynchronous UI rendering, Call this method in onAccessibilityEvent when YouTube is detected.

发布评论

评论列表(0)

  1. 暂无评论