'App open event type in Accessibility Service

I'm working on Accessibility Service, but I'm not getting events of app start. I am using TYPE_VIEW_FOCUSED currently. Please let me know how can I get events of when any app starts.

info.eventTypes = AccessibilityEvent.TYPE_VIEW_FOCUSED;


Solution 1:[1]

You can try using AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED .. It will trigger when top application has changed.

fun onAccessibilityEvent(event: AccessibilityEvent) {
    if (event.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
        event.packageName // Top application package name 
    }
}

Disclaimer - I never tried this code. I was reading about AccessibilityService since last few days. So let me know if it works or not .. i'll gladly remove this answer if its not correct. have a look at This Sample..

Solution 2:[2]

ADM's code indeed works - but just be aware that you may get more than one TYPE_WINDOW_STATE_CHANGED event for each app.

For example, when I launch Chrome, my app receives two events within milliseconds of each other. The first is for "ClassName: android.widget.FrameLayout" and the second for "ClassName: org.chromium.chrome.browser.ChromeTabbedActivity".

When I launch YouTube, my app receives four events in rapid succession: "ClassName: android.widget.FrameLayout", "ClassName: android.widget.ImageView", "ClassName: android.widget.FrameLayout", "ClassName: com.google.android.apps.youtube.app.watchwhile.WatchWhileActivity".

In either case, only one event has "FullScreen: true", so maybe you can filter on that.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ADM
Solution 2 Andy Foulke